www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unexpected Crash

reply default0 <Kevin.Labschek gmx.de> writes:
Consider the following program:

import std.stdio;

void main(string[] args) {
     int a, b, c;
     while(true) {
         readf("%d,%d,%d", &a, &b, &c);
         writeln(a, b, c);
     }
}

If I enter "5,5,5" on the commandline, hit enter, then enter 
"5,5,5" a second time, hit enter again, the second time it 
crashes. Why? What am I getting wrong here?

The error I am getting is the following:
std.conv.ConvException /usr/include/dmd/phobos/std/conv.d(2002): 
Unexpected '5' when converting from type LockingTextReader to 
type int
----------------
/usr/include/dmd/phobos/std/conv.d:58 int std.conv.parse!(int, 
std.stdio.LockingTextReader).parse(ref 
std.stdio.LockingTextReader) [0x440072]
/usr/include/dmd/phobos/std/conv.d:2226 int std.conv.parse!(int, 
std.stdio.LockingTextReader).parse(ref 
std.stdio.LockingTextReader, uint) [0x43fd63]
/usr/include/dmd/phobos/std/format.d:4383 int 
std.format.unformatValue!(int, std.stdio.LockingTextReader, 
char).unformatValue(ref std.stdio.LockingTextReader, ref 
std.format.FormatSpec!(char).FormatSpec) [0x43f529]
/usr/include/dmd/phobos/std/format.d:631 uint 
std.format.formattedRead!(std.stdio.LockingTextReader, char, 
int*, int*, int*).formattedRead(ref std.stdio.LockingTextReader, 
const(char)[], int*, int*, int*) [0x43a452]
/usr/include/dmd/phobos/std/stdio.d:1661 uint 
std.stdio.File.readf!(int*, int*, int*).readf(const(char[]), 
int*, int*, int*) [0x43a391]
/usr/include/dmd/phobos/std/stdio.d:3331 uint 
std.stdio.readf!(int*, int*, int*).readf(const(char[]), int*, 
int*, int*) [0x43a2a7]
source/app.d:47 _Dmain [0x43a253]
??:? 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv 
[0x44852a]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x448468]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x4484e6]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x448468]
??:? _d_run_main [0x4483c5]
??:? main [0x4445f7]
??:? __libc_start_main [0xdddd2a3f]
Apr 07 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 7 April 2016 at 20:42:17 UTC, default0 wrote:
 If I enter "5,5,5" on the commandline, hit enter, then enter 
 "5,5,5"
When you hit enter, that puts a \n character in the buffer. readf doesn't skip that automatically, so it complains upon hitting that newline (the error message shows the character *after* it though, which sucks). But what you want to do is to read whitespace too. I think putting a space in the format string at the beginning or end will do it (I don't use readf often though).
Apr 07 2016
parent reply default0 <Kevin.Labschek gmx.de> writes:
On Thursday, 7 April 2016 at 20:47:35 UTC, Adam D. Ruppe wrote:
 On Thursday, 7 April 2016 at 20:42:17 UTC, default0 wrote:
 If I enter "5,5,5" on the commandline, hit enter, then enter 
 "5,5,5"
When you hit enter, that puts a \n character in the buffer. readf doesn't skip that automatically, so it complains upon hitting that newline (the error message shows the character *after* it though, which sucks). But what you want to do is to read whitespace too. I think putting a space in the format string at the beginning or end will do it (I don't use readf often though).
Changing the format string to "%d,%d,%d\n" fixed the problem. Would've figured if the error complained about '\n' instead of '5'.
Apr 07 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/07/2016 01:49 PM, default0 wrote:
 On Thursday, 7 April 2016 at 20:47:35 UTC, Adam D. Ruppe wrote:
 On Thursday, 7 April 2016 at 20:42:17 UTC, default0 wrote:
 If I enter "5,5,5" on the commandline, hit enter, then enter "5,5,5"
When you hit enter, that puts a \n character in the buffer. readf doesn't skip that automatically, so it complains upon hitting that newline (the error message shows the character *after* it though, which sucks). But what you want to do is to read whitespace too. I think putting a space in the format string at the beginning or end will do it (I don't use readf often though).
Changing the format string to "%d,%d,%d\n" fixed the problem. Would've figured if the error complained about '\n' instead of '5'.
Which compiler version? This is fixed: https://issues.dlang.org/show_bug.cgi?id=15695 It's now better but \n may be hard to decode from the following message: std.conv.ConvException /usr/include/dmd/phobos/std/conv.d(2002): Unexpected ' ' when converting from type LockingTextReader to type int Ali
Apr 07 2016
parent reply default0 <Kevin.Labschek gmx.de> writes:
On Thursday, 7 April 2016 at 21:22:19 UTC, Ali Çehreli wrote:
 On 04/07/2016 01:49 PM, default0 wrote:
 On Thursday, 7 April 2016 at 20:47:35 UTC, Adam D. Ruppe wrote:
 On Thursday, 7 April 2016 at 20:42:17 UTC, default0 wrote:
 If I enter "5,5,5" on the commandline, hit enter, then enter 
 "5,5,5"
When you hit enter, that puts a \n character in the buffer. readf doesn't skip that automatically, so it complains upon hitting that newline (the error message shows the character *after* it though, which sucks). But what you want to do is to read whitespace too. I think putting a space in the format string at the beginning or end will do it (I don't use readf often though).
Changing the format string to "%d,%d,%d\n" fixed the problem. Would've figured if the error complained about '\n' instead of '5'.
Which compiler version? This is fixed: https://issues.dlang.org/show_bug.cgi?id=15695 It's now better but \n may be hard to decode from the following message: std.conv.ConvException /usr/include/dmd/phobos/std/conv.d(2002): Unexpected ' ' when converting from type LockingTextReader to type int Ali
dmd --version prints out 2.070.2 I believe 2.071 is the most recent version?
Apr 07 2016
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 04/07/2016 07:56 PM, default0 wrote:

 dmd --version prints out 2.070.2

 I believe 2.071 is the most recent version?
Yes, that's fresh out of the oven :) with potentially confusing but more correct import behaviour: http://forum.dlang.org/thread/ne1fd0$1r10$1 digitalmars.com Ali
Apr 07 2016