import std.stdio;
void main(){
int n;
while(readf("%d", &n)){
if(n == 11)
break;
writeln(n);
}
}
The first iteration works, and it prints n
, but after that readf()
never returns.
The documentation has only a single line explaining readf()
:
uint readf(A...)(in char[] format, A args);
Formatted read one line from stdin.
Am I do something wrong? or is there something wrong with readf()
? I just need to read numbers from the standard input.
using: DMD 2.054 64-bit
I believe it's because
readf
handles spaces differently thanscanf
in C. You need to explicitly read in the spaces, so changereadf("%d", &n)
toreadf("%d ", &n)
and it should work (hopefully).Here's a quote from Andrei, who implemented the function:
http://www.digitalmars.com/d/archives/digitalmars/D/bugs/Issue_4656_New_stdio.readf_does_not_ignore_white_space_24214.html