[英]how does this code from “The C Programming Language” work?
I'm reading "The C Programming Language (2nd ed.) and near the beginning, it has examples like this: 我正在阅读“ The C Programming Language(第二版)”,在一开始的时候,它有这样的示例:
while((c = getchar()) != EOF)
if(c == '\n'){
++n1;
I can see how this would work while reading from a file, and I understand this syntax... But this is just reading from the console--how does one signal end of file when entering characters from a console? 我可以看到在读取文件时这是如何工作的,并且我了解这种语法...但是,这只是从控制台读取的内容-从控制台输入字符时,信号如何指示文件结尾? I'm using Windows XP... MinGW compiler... Anyways, was this book written for waaay earlier systems with like an EOF button or something?
我正在使用Windows XP ... MinGW编译器...无论如何,这本书是为waaay早期系统编写的,具有EOF按钮之类的功能?
well, I have one more question, that's just related to how the end-of-file works under Windows. 好吧,我还有一个问题,那就是与文件结束在Windows下的工作方式有关。
If I just while(getchar()!=EOF);
如果我只是
while(getchar()!=EOF);
, then I can just keep typing characters until I signal EOF via ^Z. ,那么我可以继续输入字符,直到通过^ Z发出EOF信号为止。 But, I have to write a newline, then hit ^Z, then another newline... Why does it have to be on its own line?
但是,我必须写一个换行符,然后按^ Z,然后再输入另一个换行符...为什么必须在自己的行上?
Windows uses Ctrl-Z for EOF, and UNIX uses Ctrl-D. Windows将Ctrl-Z用于EOF,而UNIX使用Ctrl-D。 See http://bytes.com/groups/c/217873-eof-windows , and excellent book choice.
请参阅http://bytes.com/groups/c/217873-eof-windows和出色的书籍选择。 :)
:)
^ Z为EOF。
The correct answer has been already given, but a typical usage would be to redirect a file to standard output: 已经给出了正确的答案,但是典型的用法是将文件重定向到标准输出:
program.exe < samplefile.txt
samplefile.txt
is "written" to standard out and program.exe
reads this from standard out until the EOF is reached. samplefile.txt
被“写入”到标准输出中, program.exe
从标准输出中读取该文件,直到达到EOF。
Regarding your question on ^Z, the reasonn it behaves like this is because it isn't really a character, it's a signal from the operating system to the C input system. 关于您在^ Z上的问题,其行为之所以如此,是因为它实际上不是字符,它是从操作系统到C输入系统的信号。 As such, it is highly dependant on the interaction between the OS and the C input system buffering.
因此,它高度依赖于OS和C输入系统缓冲之间的交互。 Which is a fancy way of saying that it's just the way things work, for Windows and for your particular C implementation.
这是一种奇妙的说法,它只是Windows和您的特定C实现的工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.