问题
Im using scanf because we must use it. the problem is the following : (thats just an example of the problem):
int main() {
char ch [10]={0};
scanf("%s",ch);
printf("%s",ch);
}
if i run the program and enter for example : word^Z when ^Z is EOF. the program stays in place, stuck in the scanf, althogh i did type word then ctrl+z then Enter. but somehow it stays in the scanf, its the same thing with redirection, like its not a problem with ctr+z or anything.
i hope that i can get some help
thanks in advance, totally apprecaite it :)
回答1:
scanf
uses whitespace as a delimiter to store the read data into various fields. From the command line, entering ControlZ, then Enter only puts the EOF
character into the input stream and scanf()
continues waiting for whitespace. If you hit Enter again, scanf
will receive the whitespace character, and everything including the EOF
will be stored into the ch
array.
Here's a sample run. The first line is the input, and the second line is the output.
Hello^Z
Hello→
来源:https://stackoverflow.com/questions/14313344/scanfs-an-issue-with-eof