与fgets的返回值() [英] Return value of fgets()

查看:662
本文介绍了与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 in 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.

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 than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF 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() and fgets() return s on success, and NULL 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 return NULL. Indeed it says a NULL would only occur where EOF 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 return s. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgets() 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 set errno to indicate the error.

This clearly indicates it's only going to return a NULL if it's actually at EOF when called, i.e. if any bytes are read, it won't return NULL.

这篇关于与fgets的返回值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆