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

查看:38
本文介绍了fgets() 的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚刚开始在 C 中使用 I/O.这是我的问题 -
我有一个文件,我从中读取了我的输入.然后我使用 fgets() 在我以某种方式利用的缓冲区中获取字符串.现在,如果输入对于缓冲区来说太短会发生什么,即如果 fgets() 的第一次读取到达 EOF.fgets() 是否应该返回 NULL(正如我在 fgets() 文档中所读到的)?似乎没有,我正确地得到了我的输入.此外,即使我的 feof(input) 也没有说我们已经达到了 EOF.
这是我的代码片段.

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.
", argv[argc + 1]);
    exit(EXIT_FAILURE);
}

if (output == NULL) {
    fprintf(stdout, "Failed to open file - %s.
", 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.
");
}
else if (ferror(input)) {
    fprintf(stdout, "Error while reading the file.
");
}

推荐答案

fgets() 的文档并没有说明你的想法:

The documentation for fgets() does not say what you think it does:

来自我的手册页

fgets() 从流中读取至多小于 size 个字符,并将它们存储到 s 指向的缓冲区中.阅读在 EOF 或换行符后停止.如果读取换行符,则将其存储到缓冲区中.存储终止空字节 ('')在缓冲区中的最后一个字符之后.

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 ('') is stored after the last character in the buffer.

以后

gets()fgets() 在成功时返回 s,在错误或结束时返回 NULL未读取任何字符时出现文件错误.

gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.

我不认为 EOF 将被视为错误条件并返回 NULL.事实上,它说 NULL 只会在 EOF 出现的地方出现,而 没有 字符被读取.

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.

POSIX 标准(遵循不易访问的 C 标准)在这里:http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html 并指出:

The POSIX standard (which defers to the less accessible C standard) is here: http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html and states:

成功完成后,fgets() 将返回 s.如果流位于文件尾,则应设置流的文件尾指示符,并且 fgets() 应返回空指针.如果发生读取错误,则应设置流的错误指示符,fgets() 应返回空指针,并应设置 errno 以指示错误.

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.

这清楚地表明它只会返回一个 NULL 如果它在调用时实际上位于 EOF,即如果 任何 字节被读取,它不会返回 NULL.

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天全站免登陆