CTRL-D并没有停止一段时间(的getchar()!= EOF)循环 [英] ctrl-d didn't stop the while(getchar()!=EOF) loop
问题描述
下面是我的code。我与终端运行在Ubuntu。当我输入(A <大骨节病>控制骨节病> <大骨节病> D 骨节病>)的终端,该计划并没有停止,而是继续等待我的输入。
不是<大骨节病>控制骨节病> <大骨节病> D 骨节病>等于EOF在UNIX?
感谢您。
#包括LT&;&stdio.h中GT;主(){
INT D组;
而(D =的getchar()!= EOF){
的printf(\\的getchar()= EOF \\的结果为%d \\ n,D!);
的printf(EOF数:%d \\ n,EOF);
}
的printf(\\的getchar()= EOF \\的结果为%d \\ n,D!);
}
EOF不是字符。在 EOF
是一个宏,的getchar()
的回报,当它到达输入的结尾或遇到某种错误。在 ^ D
不是EOF字符。发生了什么事在linux下当你打^ D上本身就是一个线是它关闭流,而的getchar()
呼叫到达输入的结尾,并返回 EOF
宏。如果您键入 ^ D
某处线的中间,流没有关闭,所以的getchar()
返回它读取的价值观和你的循环不会退出。
此外:
在现代系统中,它没有反映存储在文件中任何实际的档案结尾字符;这是一个信号,即没有更多的字符可供选择。
块引用>Here is my code. I run it in ubuntu with terminal. when I type (a CtrlD) in terminal, the program didn't stop but continued to wait for my input.
Isn't CtrlD equal to EOF in unix?
Thank you.
#include<stdio.h> main() { int d; while(d=getchar()!=EOF) { printf("\"getchar()!=EOF\" result is %d\n", d); printf("EOF:%d\n", EOF); } printf("\"getchar()!=EOF\" result is %d\n", d); }
解决方案EOF is not a character. The
EOF
is a macro thatgetchar()
returns when it reaches the end of input or encounters some kind of error. The^D
is not "an EOF character". What's happening under linux when you hit ^D on a line by itself is that it closes the stream, and thegetchar()
call reaches the end of input and returns theEOF
macro. If you type^D
somewhere in the middle of a line, the stream isn't closed, sogetchar()
returns values that it read and your loop doesn't exit.See the stdio section of the C faq for a better description.
Additionally:
On modern systems, it does not reflect any actual end-of-file character stored in a file; it is a signal that no more characters are available.
这篇关于CTRL-D并没有停止一段时间(的getchar()!= EOF)循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!