scanf(%s)EOF问题 [英] scanf(%s) an issue with EOF

查看:472
本文介绍了scanf(%s)EOF问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scanf,因为我们必须使用它。
问题如下:
(那只是问题的一个示例):

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);
    }

如果我运行程序并输入例如:word ^ Z $ b $当^ Z为EOF时b。
程序保持原位,卡在scanf中,虽然我确实输入了单词,然后按ctrl + z,然后按Enter。但是不知何故,它仍然保留在scanf中,与重定向相同,例如,它与ctr + z或其他任何问题无关。

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 :)

推荐答案

scanf 使用空格作为分隔符,将读取的数据存储到各个字段中。在命令行中,输入 Control Z ,然后 Enter 仅将 EOF 字符放入进入输入流, scanf()继续等待空白。如果再次按 Enter ,则 scanf 将收到空白字符,包括 EOF 将存储到 ch 数组中。

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→

这篇关于scanf(%s)EOF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆