问题
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
回答1:
I believe it's because readf
handles spaces differently than scanf
in C. You need to explicitly read in the spaces, so change readf("%d", &n)
to readf("%d ", &n)
and it should work (hopefully).
Here's a quote from Andrei, who implemented the function:
This is by design. The example works when modified as follows:
import std.stdio;
void main() {
int i, j;
readf("%s", &i);
readf(" %s", &j);
}The space before the second parameter tells readf to read and skip all whitespace before attempting conversion.
I've implemented readf to be a fair amount more Nazi about whitespace than scanf in an attempt to improve its precision. Scanf has been famously difficult to use for complex input parsing and validation, and I attribute some of that to its laissez-faire attitude toward whitespace. I'd be glad to relax some of readf's insistence on precise whitespace handling if there's enough evidence that that serves most of our users. I personally believe that the current behavior (strict by default, easy to relax) is best.
http://www.digitalmars.com/d/archives/digitalmars/D/bugs/Issue_4656_New_stdio.readf_does_not_ignore_white_space_24214.html
来源:https://stackoverflow.com/questions/7118734/why-is-readf-not-behaving-as-expected