C语言 fgets() 的返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21679063/
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
Return value of fgets()
提问by yadav_vi
I have just recently started working with I/Oin C. Here is my question -
I have a file, from which I read my input. Then I use fgets()to get strings in a buffer which I utilise in some way. Now, what happens if the input is too short for the buffer i.e. if the first read by fgets()reaches EOF. Should fgets()return NULL(as I have read in fgets()documentation)? It seems that it doesn't and I get my input properly. Besides even my feof(input)does not say that we have reached EOF.
Here is my code snippet.
我最近才开始I/O在C. 这是我的问题 -
我有一个文件,我从中读取了我的输入。然后我用来fgets()在我以某种方式利用的缓冲区中获取字符串。现在,如果输入对于缓冲区来说太短会发生什么,即如果第一个读取fgets()到达EOF。应该fgets()返回NULL(正如我在fgets()文档中读到的那样)?似乎没有,我正确地得到了我的输入。再说连我feof(input)都不说,我们已经达到了EOF。
这是我的代码片段。
char buf[BUFSIZ];
FILE *input,
*output;
input = fopen(argv[--argc], "r");
output = fopen(argv[--argc], "w");
/**
* If either of the input or output were unable to be opened
* we exit
*/
if (input == NULL) {
fprintf(stdout, "Failed to open file - %s.\n", argv[argc + 1]);
exit(EXIT_FAILURE);
}
if (output == NULL) {
fprintf(stdout, "Failed to open file - %s.\n", argv[argc + 0]);
exit(EXIT_FAILURE);
}
if (fgets(buf, sizeof(buf), input) != NULL) {
....
}
/**
* After the fgets() condition exits it is because, either -
* 1) The EOF was reached.
* 2) There is a read error.
*/
if (feof(input)) {
fprintf(stdout, "Reached EOF.\n");
}
else if (ferror(input)) {
fprintf(stdout, "Error while reading the file.\n");
}
回答by abligh
The documentation for fgets()does not say what you think it does:
的文档fgets()没有说明您认为它的作用:
From my manpage
从我的联机帮助页
fgets()reads in at most one less thansizecharacters from stream and stores them into the buffer pointed to bys. Reading stops after anEOFor a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
fgets()size从流中读取至多比字符少 1并将它们存储到由 指向的缓冲区中s。阅读在EOF换行或换行后停止。如果读取换行符,则将其存储到缓冲区中。终止空字节 ('\0') 存储在缓冲区中的最后一个字符之后。
And later
然后
gets()andfgets()returnson success, andNULLon error or when end of file occurs while no characters have been read.
gets()并在成功、错误或文件结束时fgets()返回s,NULL而没有读取任何字符。
I don't read that as saying an EOFwill be treated as an error condition and return NULL. Indeed it says a NULLwould only occur where EOFoccurs when nocharacters have been read.
我不认为这EOF会被视为错误条件并返回NULL。事实上,它说 aNULL只会发生在没有字符被读取EOF时发生的地方。
The POSIX standard (which defers to the less accessible C standard) is here: http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.htmland states:
POSIX 标准(遵循不易访问的 C 标准)在这里:http: //pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html并指出:
Upon successful completion,
fgets()shall returns. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set andfgets()shall return a null pointer. If a read error occurs, the error indicator for the stream shall be set,fgets()shall return a null pointer, and shall seterrnoto indicate the error.
成功完成后,
fgets()应返回s。如果流位于文件尾,则应设置流的文件尾指示符并fgets()返回空指针。如果发生读取错误,则应设置流的错误指示符,fgets()应返回空指针,并应设置errno以指示错误。
This clearly indicates it's only going to return a NULLif it's actually at EOFwhen called, i.e. if anybytes are read, it won't return NULL.
这清楚地表明它只会返回 aNULL如果它实际上在EOF被调用时,即如果任何字节被读取,它不会返回NULL。

