scanf(%s) an issue with EOF

蹲街弑〆低调 提交于 2019-12-13 05:29:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!