Comment #0 by bearophile_hugs — 2011-08-25T02:53:20Z
import std.stdio: readf, writef, writeln;
void main() {
int x = 5;
writef("Give x: ");
readf("%d\n", &x);
writeln("\nx: ", x);
}
If there is no input (because that program is called by another one), readf leaves x unchanged and doesn't raise an exception. I think this is not good.
-------------
Another problem: on Windows if you run that program from the console, and you just hit enter, you are allowed to keep giving lines. Is this by design?
...>test
Give x:
555
123
155
Comment #1 by schveiguy — 2011-08-25T04:19:55Z
readf returns the number of arguments filled in.
I don't think an exception is the correct path -- that is too assuming of readf.
I think the second problem you raise is a valid concern.
Comment #2 by lovelydear — 2012-04-27T10:01:10Z
With 2.059, execution gives:
PS E:\DigitalMars\dmd2\samples> E:\DigitalMars\dmd2\samples\bug.exe
Give x: 5
x: 5
PS E:\DigitalMars\dmd2\samples> E:\DigitalMars\dmd2\samples\bug.exe
Give x:
std.conv.ConvException@E:\DigitalMars\dmd2\windows\bin\..\..\src\phobos\std\conv.d(1779): Unexpected '
' when converting from type LockingTextReader to type int
----------------
414878
414703
4055E1
40536F
4050BE
404990
4048F4
404849
402040
406E28
406E62
406A83
41BB1D
----------------
PS E:\DigitalMars\dmd2\samples>
Comment #3 by bearophile_hugs — 2012-05-22T15:16:31Z
The second problem is now fixed.
Regarding the first problem, I don't like a lot the fact that readf doesn't throw an exception. But something like this seems to fix my problem, so I close this bug report:
enforce(readf("%d\n", &x));