与fgets的返回值() [英] Return value of fgets()
问题描述
我最近刚开始在 C
与 I / O
工作。这里是我的问题 -
我有一个文件,从中我看到我的输入。然后,我用与fgets()
来得到我以某种方式利用缓冲区的字符串。现在,如果输入的是太短了缓冲,即如果第一次通过与fgets读,会发生什么()
达到 EOF
。如若与fgets()
收益 NULL
(正如我在已阅读与fgets()
文档)?它似乎不和我正确地得到我的意见。除了连我的的feof(输入)
不说,我们已经达到了 EOF
。
这里是我的code片段。
字符BUF [BUFSIZ];
FILE *输入,
*输出;输入= FOPEN(的argv [ - ARGC],R);
输出= FOPEN(的argv [ - ARGC],W);/ **
*如果输入或输出的无法被打开
*我们退出
* /
如果(输入== NULL){
fprintf中(标准输出,无法打开文件 - %S \\ N的argv [ARGC + 1]);
出口(EXIT_FAILURE);
}如果(输出== NULL){
fprintf中(标准输出,无法打开文件 - %S \\ N的argv [ARGC + 0]);
出口(EXIT_FAILURE);
}如果(与fgets(buf中,sizeof的(BUF),输入)!= NULL){
....
}/ **
*在与fgets()条件,即可退出这是因为,无论是后 -
* 1)EOF达到。
* 2)有一个读误差。
* /
如果(的feof(输入)){
fprintf中(标准输出,达到EOF \\ n);
}
否则如果(FERROR(输入)){
fprintf中(标准输出,错误读取文件\\ n);
}
的文档与fgets()
不说什么你认为它的作用:
从我的手册页
与fgets()
至多有一个小于读取尺寸
从流并将它们存储字符到缓冲区指出通过取值
。读
一个EOF
或换行符后停止。如果一个新行被读取时,它被存储到缓冲器中。终止空字节(\\
0)被存储
后在缓冲器中的最后一个字符。
块引用>和后来
获得()
和与fgets()
收益取值
上的成功,而NULL
上的错误或者当没有字符被读取时的文件结束。
块引用>我不读了的话说,一个
EOF
将被视为一个错误条件并返回NULL
。事实上,它说,一个NULL
只会发生在哪里EOF
时发生的没有的角色已读POSIX标准(会推迟到不易进入的C标准)是在这里: http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html 并指出:
成功完成后,
与fgets()
将返回取值
。如果流是在结束文件,流最终的文件指示应设置和与fgets()
将返回一个空指针。如果发生读取错误,流的错误指示器应设置,与fgets()
将返回一个空指针,并应设置错误号
来指示错误。
块引用>这清楚地表明它一定会返回一个
NULL
如果它实际上是在EOF
调用的时候,也就是说,如果任何的读取字节,它不会返回NULL
。I have just recently started working with
I/O
inC
. Here is my question -
I have a file, from which I read my input. Then I usefgets()
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 byfgets()
reachesEOF
. Shouldfgets()
returnNULL
(as I have read infgets()
documentation)? It seems that it doesn't and I get my input properly. Besides even myfeof(input)
does not say that we have reachedEOF
.
Here is my code snippet.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"); }
解决方案The documentation for
fgets()
does not say what you think it does:From my manpage
fgets()
reads in at most one less thansize
characters from stream and stores them into the buffer pointed to bys
. Reading stops after anEOF
or 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.And later
gets()
andfgets()
returns
on success, andNULL
on error or when end of file occurs while no characters have been read.I don't read that as saying an
EOF
will be treated as an error condition and returnNULL
. Indeed it says aNULL
would only occur whereEOF
occurs when no characters have been read.The POSIX standard (which defers to the less accessible C standard) is here: http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html and states:
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 seterrno
to indicate the error.This clearly indicates it's only going to return a
NULL
if it's actually atEOF
when called, i.e. if any bytes are read, it won't returnNULL
.这篇关于与fgets的返回值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!