是否可以区分fgets返回的错误 [英] Is it possible to distinguish the error returned by fgets

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

问题描述

在查看 fgets §7.21.7.2,3的ISO C11标准后,返回值是关于概要代码的说明:

Upon looking at the ISO C11 standard for fgets §7.21.7.2, 3, the return value is stated regarding the synopsis code:

#include <stdio.h>
char *fgets(char* restrict s, int n, FILE* restrict stream);



fgets 函数返回 s 如果成功。如果遇到文件末尾并且没有字符读入数组,则数组的内容保持不变,并返回空指针。如果在操作过程中发生读取错误,则数组内容不确定并且返回空指针。

The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.

标准指出,对于文件结尾且未读取任何字符,或者发生读取错误。我的问题是,仅来自 fgets 和返回的空指针,是否有办法区分这两种情况中的哪一种导致了错误?

The standard says that a null pointer is returned for either an end-of-file and no characters have been read in or a read error occurs. My question is, just from fgets, and the returned null pointer, is there a way to distinguish which of the two cases caused the error?

推荐答案


是否可以区分两种情况中的哪一种导致了错误?

Is there a way to distinguish which of the two cases caused the error?

是的,使用 feof() ferror()进行区分。 @什么都没有

Yes, use feof() and ferror() to distinguish. @Nothing Nothing

但是正确使用很重要。考虑以下两个代码:

Yet it is important to use correctly. Consider the two codes:

char buf[100];
fgets(s, sizeof s, stream);
if (feof(stream)) return "End-of-file occurred";
if (ferror(stream)) return "Input error occurred"; 


if (fgets(s, sizeof s, stream) == NULL) {
  if (feof(stream)) return "End-of-file occurred";
  if (ferror(stream)) return "Input error occurred"; 
  return "Should never get here";
}

第二个针对 NULL <正确测试返回值/ code>,如OP所建议。

The second properly tests the return value against NULL, as suggested by OP.

第一个遇到一个罕见的问题。 ferror(stream)测试一个标志。此标志可能是由先前上的I / O函数调用设置的,因此此 fgets()不一定是错误的原因。最好检查 fgets()的结果,以查看 this 函数是否失败。

The first can encounter a rare problem. The ferror(stream) tests a flag. This flag may have been set by a prior I/O function call on stream so this fgets() is not necessarily the cause of the error. Best to check the result of fgets() to see if this function failed.

如果代码要在检测到错误后继续使用 stream ,请确保在继续操作之前清除错误-如

If code is to continue using stream after an error detected, be sure to clear the error before continuing - like maybe to attempt a re-try.

if (ferror(stream)) {   
  clearerr(stream);
  return "Input error occurred");
}

请注意, clearerr()清除错误和文件结束标志。

Note that clearerr() clears both the error and end-of-file flags.

feof()同样适用,但是,一旦文件末尾为真,大多数代码将使用 stream 退出。

The same applies for feof(), yet most code is written to quit using stream once an end-of-file is true.

有一种第三种 pathological 方式来接收 NULL 而没有 feof() ferror()返回 NULL ,如 fgets()返回的NULL是否符合兼容短缓冲区的要求?。仔细阅读C规范会有3个 if,其中可能没有一个是正确的,因此缺少规范-这意味着UB。

There is a 3rd pathological way to receive NULL and neither feof() nor ferror() returns NULL as detailed in Is fgets() returning NULL with a short buffer compliant?. Careful reading of the C spec has 3 "ifs", of which it is possible that not of them are true as so the spec is lacking - which implies UB.

这篇关于是否可以区分fgets返回的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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