摆脱编译器警告"warning: result of call is not used"
Get rid of compiler warning "warning: result of call is not used"
我正在编写一些遗留代码,其中包括以下函数。总之,该函数从文本文件中读取一行,去掉前导/尾随空格和换行符,检查错误,成功时返回字符串中的字符数,错误时返回-1。你能给我一些摆脱警告的提示吗?我知道这不是一个错误,但我想改进代码。谢谢
static int readline(file *mf, char *buf, int n, int strip) {
if (!buf || n < 1 || !mf) return seterror(MDIO_BADPARAMS);
// Read the line
fgets(buf, n, mf->f);
// End of file reached?
if (feof(mf->f)) return seterror(MDIO_EOF);
// File I/O error?
if (ferror(mf->f)) return seterror(MDIO_IOERROR);
// comment line?
if (buf[0] == '#') return readline(mf,buf,n,strip);
// Strip whitespace
if (strip) strip_white(buf);
return strlen(buf);
}
该函数可以编译(nvcc),但它有警告:
警告:未使用调用结果
fgets
是用warn_unused_result
属性声明的。不检查结果通常是一个编程错误:如果fgets
未能读取任何内容,它将返回NULL并保持缓冲区不变。如果您不检查这种情况,您可能会处理过时或未初始化的数据。
要解决此问题,只需检查结果:
if(!fgets(buf, n, mf->f)) return seterror(MDIO_EOF);
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- Usages of std::move
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 具有变量Number of Arguments的std::函数的矢量
- Capacity of a deque
- Deprecation of _writeBarrier()
- constexpr begin of a std::array
- 如何调整 std::vector of Eigen::MatrixXd 的大小
- (C++)构造函数、默认参数"call of overloaded... ambigous"
- 为什么海湾合作委员会抱怨"declaration of 'foo' shadows a previous call [-Werror=shadow]"
- 为什么这句话"The expression can be used only as the left-hand operand of a member function call"在 [expr.re
- "Call to implicitly deleted default constructor of "错误
- 摆脱编译器警告"warning: result of call is not used"
- C++ "call of overloaded is ambiguous"在英足总
- "is not a member of"和"no matching function for call to"错误
- shared_ptr : "call of an object of a class type without appropriate operator() or conversion functio
- "The value of ESP was not properly saved across a function call."问题
- 奇怪的 MSC 8.0 错误:"The value of ESP was not properly saved across a function call..."