fread() 或 fwrite() 是否可能返回一个大于项目数的值?为什么?

huangapple go评论64阅读模式
英文:

Is it possible for fread() or fwrite() to return a value greater than the number of items? Why?

问题

Reading through https://en.cppreference.com/w/c/io/fread, I noticed that the result of fread() is compared with ==. I saw people use < before, so the obvious question presented itself of whether the error value can be MORE than the number of items that were asked of the function?

fread/fwrite的man页面说:

如果发生错误或到达文件的结尾,返回值是短项目计数(或零)。

This is a strong implication that it might be true, but I would prefer a more official confirmation if possible, or at least a confirmation that what the man page says can be taken seriously.

英文:

Reading through https://en.cppreference.com/w/c/io/fread , I noticed that the result of fread() is compared with ==. I saw people use < before, so the obvious question presented itself of whether the error value can be MORE than the number of items that were asked of the function?

The man page for fread/fwrite says:
> If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

This is a strong implication that it might be true, but I would prefer a more official confirmation if possible, or at least a confirmation that what the man page says can be taken seriously.

答案1

得分: 4

【§7.21.8.1 §3 of the ISO C11 standard】(http://port70.net/~nsz/c/c11/n1570.html#7.21.8.1p3) 规定如下:

fread函数返回成功读取的元素数量,如果遇到读取错误或文件结束,可能小于nmemb。

因此,返回值永远不会超过第三个函数参数,即nmemb的值。

英文:

§7.21.8.1 ¶3 of the ISO C11 standard states the following:

> The fread function returns the number of elements successfully read, which may be less than nmemb if a read error or end-of-file is encountered.

Therefore, the return value will never exceed the value of the nmemb function argument (which is the third function argument).

答案2

得分: 2

你没有为这段文字提供引用:

> 如果发生错误,或者达到文件的末尾,返回值是一个短的项目计数(或零)。

这并不在你提供的C++参考页面中。它在die.net关于fread的页面以及macOS 10.14.6的man页面中可以找到。然而,这些页面写得不够准确。例如,它们说:“函数fread()读取nmemb个数据元素,…”这在一般情况下显然是错误的。更准确和正确地说,fread 尝试 读取nmemb个数据元素。只有在发生错误时它才会停止。

无论如何,fread不会读取比请求的元素更多,因此它不会返回大于nmemb参数的结果。

英文:

You do not give a citation for this text:

> If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

It is not in the C++ reference page you link to. It is in the die.net page for fread and in the macOS 10.14.6 man page for fread. However, these pages are imprecisely written. For example, they say “The function fread() reads nmemb elements of data,…” which is of course wrong in general. More precisely and correctly, fread attempts to read nmemb elements of data. It stops when an error occurs.

In any case, fread will not read more elements than requested, and therefore it will not return a result greater than the nmemb argument.

答案3

得分: 1

"在使用fread()fwrite()时,是否可能返回大于项数的值?

正确输入参数

不可能,正如其他人已经解释的那样。


错误输入参数

在输入参数不正确的情况下,可能会出现大于项数的值。

char buf[255];

// 对于有符号字符,`number_of_items` 可能等于 -1
char number_of_items = sizeof buf;

// 将 -1 传递给 `fread()` 就像传递 `SIZE_MAX` 一样。
// 代码会出现 UB(未定义行为),读取超过 255 个字符。
size_t number_read = fread(buf, 1, number_of_items, stream);

if (number_read > sizeof buf) {
  puts("出错了");
}
英文:

> Is it possible for fread() or fwrite() to return a value greater than the number of items?

Correctly typed arguments

No, as others well explained.


Incorrectly typed arguments

With poorly typed code, it can seem like a value greater than the number of items.

char buf[255];

// With signed char, `number_of_items` may equal -1
char number_of_items = sizeof buf;

// Passing -1 to `fread()` is like passing `SIZE_MAX`.
// Code experiences UB reading more than 255 characters.
size_t number_read = fread(buf, 1, number_of_items, stream);

if (number_read > sizeof buf) {
  puts("Oops");
}

huangapple
  • 本文由 发表于 2023年5月18日 00:51:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定