C语言 C 中的文件结尾 (EOF)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4358728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:18:17  来源:igfitidea点击:

End of File (EOF) in C

ceof

提问by newbie

I am currently reading the book C Programming Language by Ritchie & Kernighan. And I am pretty confused about the usage of EOF in the getchar()function.

我目前正在阅读 Ritchie & Kernighan 所著的《C 编程语言》一书。我对getchar()函数中 EOF 的使用感到非常困惑。

First, I want to know why the value of EOF is -1 and why the value of getchar()!=EOFis 0. Pardon me for my question but I really don't understand. I really tried but I can't.

首先,我想知道为什么EOF的值为-1,为什么值为getchar()!=EOF0。请原谅我的问题,但我真的不明白。我真的试过了,但我做不到。

Then I tried to run the example on the book that can count the number of characters using the code below but it seems that I never get out of the loop even if I press enter so I am wondering when would I reach the EOF?

然后我尝试运行书中的示例,该示例可以使用下面的代码计算字符数,但似乎即使我按 Enter 也永远不会退出循环,所以我想知道什么时候才能到达 EOF?

main(){
   long nc;
   nc = 0;
   while (getchar() != EOF)
       ++nc;
   printf("%ld\n", nc);
}

Then, I read the same problem at Problem with EOF in C. Most people advised that instead of using EOF, use the terminator \n or the null terminator '\0' which makes a lot of sense.

然后,我在Problem with EOF in C 中阅读了同样的问题。大多数人建议不要使用 EOF,而是使用终止符 \n 或空终止符 '\0' 这很有意义。

Does it mean that the example on the book serves another purpose?

这是否意味着书中的例子另有用途?

回答by Steve Jessop

EOF indicates "end of file". A newline (which is what happens when you press enter) isn't the end of a file, it's the end of a line, so a newline doesn't terminate this loop.

EOF 表示“文件结束”。换行符(也就是当你按回车键会发生什么)不是结束文件,这是一个结束,因此换行符不会终止这个循环。

The code isn't wrong[*], it just doesn't do what you seem to expect. It reads to the end of the input, but you seem to want to read only to the end of a line.

代码没有错[*],它只是没有按照您的预期运行。它读取到输入的末尾,但您似乎只想读取到一行的末尾。

The value of EOF is -1 because it has to be different from any return value from getcharthat is an actual character. So getcharreturns any character value as an unsigned char, converted to int, which will therefore be non-negative.

EOF 的值为 -1,因为它必须与任何返回值不同,因为它getchar是实际字符。因此,getchar将任何字符值作为无符号字符返回,转换为 int,因此将是非负数。

If you're typing at the terminal and you want to provoke an end-of-file, use CTRL-D (unix-style systems) or CTRL-Z (Windows). Then after all the input has been read, getchar()will return EOF, and hence getchar() != EOFwill be false, and the loop will terminate.

如果您在终端打字并且想要触发文件结尾,请使用 CTRL-D(unix 样式系统)或 CTRL-Z(Windows)。然后在读取所有输入后,getchar()将返回EOF,因此getchar() != EOF为假,循环将终止。

[*] well, it has undefined behavior if the input is more than LONG_MAX characters due to integer overflow, but we can probably forgive that in a simple example.

[*] 好吧,如果输入由于整数溢出而超过 LONG_MAX 个字符,则它具有未定义的行为,但我们可能可以在一个简单的示例中原谅这一点。

回答by Karl Knechtel

EOF is -1 because that's how it's defined. The name is provided by the standard library headers that you #include. They make it equal to -1 because it has to be something that can't be mistaken for an actual byte read by getchar(). getchar()reports the values of actual bytes using positive number (0 up to 255 inclusive), so -1 works fine for this.

EOF 是 -1,因为它是这样定义的。该名称由您提供的标准库头文件提供#include。他们使它等于 -1,因为它必须是不能被误认为由getchar(). getchar()使用正数(0 到 255 包括在内)报告实际字节的值,因此 -1 可以正常工作。

The !=operator means "not equal". 0 stands for false, and anything else stands for true. So what happens is, we call the getchar()function, and compare the result to -1 (EOF). If the result was not equal to EOF, then the result is true, because things that are not equal are not equal. If the result was equal to EOF, then the result is false, because things that are equal are not (not equal).

!=运营商表示“不等于”。0 代表假,其他的都代表真。所以发生的事情是,我们调用该getchar()函数,并将结果与​​ -1 (EOF) 进行比较。如果结果不等于 EOF,则结果为真,因为不相等的事物不相等。如果结果等于 EOF,则结果为假,因为相等的事物不相等(不相等)。

The call to getchar()returns EOF when you reach the "end of file". As far as C is concerned, the 'standard input' (the data you are giving to your program by typing in the command window) is just like a file. Of course, you can always type more, so you need an explicit way to say "I'm done". On Windows systems, this is control-Z. On Unix systems, this is control-D.

getchar()当您到达“文件结尾”时,调用返回 EOF。就 C 而言,“标准输入”(您通过在命令窗口中键入而提供给程序的数据)就像一个文件。当然,您总是可以输入更多内容,因此您需要一种明确的方式来说“我完成了”。在 Windows 系统上,这是 control-Z。在 Unix 系统上,这是 control-D。

The example in the book is not "wrong". It depends on what you actually want to do. Reading until EOF means that you read everything, until the user says "I'm done", and then you can't read any more. Reading until '\n' means that you read a line of input. Reading until '\0' is a bad idea if you expect the user to type the input, because it is either hard or impossible to produce this byte with a keyboard at the command prompt :)

书中的例子并没有“错”。这取决于您实际想要做什么。阅读到 EOF 意味着你阅读了所有内容,直到用户说“我完成了”,然后你就不能再阅读了。读到 '\n' 意味着你读了一行输入。如果您希望用户键入输入,则读取直到 '\0' 是一个坏主意,因为在命令提示符下使用键盘很难或不可能生成此字节:)

回答by Drakosha

That's a lot of questions.

这是很多问题。

  1. Why EOFis -1: usually -1 in POSIX system calls is returned on error, so i guess the idea is "EOF is kind of error"

  2. any boolean operation (including !=) returns 1 in case it's TRUE, and 0 in case it's FALSE, so getchar() != EOFis 0when it's FALSE, meaning getchar()returned EOF.

  3. in order to emulate EOFwhen reading from stdinpress Ctrl+D

  1. 为什么EOF是 -1:通常 POSIX 系统调用中的 -1 在错误时返回,所以我猜这个想法是“EOF 是一种错误”

  2. 任何布尔运算(包括 !=)在为 TRUE 的情况下返回 1,在为 FALSE 的情况下返回 0,因此getchar() != EOF0FALSE 时,意味着getchar()返回EOF

  3. 为了EOF在阅读stdin新闻时效仿Ctrl+D