C语言 c 中的 fread() 返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28398255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:44:17  来源:igfitidea点击:

fread() return value in C

c

提问by Jordan Camp

I am trying to understand how the fread()function in <stdio.h>works and I am confused about the return value of this function. In the man pages it says

我试图了解该fread()函数的<stdio.h>工作原理,但我对该函数的返回值感到困惑。在手册页中它说

RETURN VALUE
On success, fread()and fwrite()return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

fread()does not distinguish between end-of-file and error, and callers must use feof(3)and ferror(3)to determine which occurred.

返回值
如果成功,fread()并且fwrite()返回的项目数读取或写入。此数字等于仅当 size 为 1 时传输的字节数。如果发生错误或到达文件末尾,则返回值是一个短项目计数(或零)。

fread()不区分文件结束和错误,调用者必须使用feof(3)ferror(3)来确定发生了什么。

Could someone please explain to me what is meant by number of items read or writtenin this context. Also can anyone provide me with some example return values and their meanings?

有人可以向我解释一下number of items read or written在这种情况下的含义。也有人可以为我提供一些示例返回值及其含义吗?

回答by moooeeeep

fread()will read a countof items of a specified sizefrom the provided IO stream (FILE* stream). It returns the number of items successfully readfrom the stream. If it returns a number less than the requested number of items, the IO stream can be considered empty (or otherwise broken).

fread()将从提供的 IO 流 ( ) 中读取指定大小的项目计数。它返回从流中成功读取的项目数。如果它返回的数字小于请求的项目数,则可以认为 IO 流为空(或以其他方式损坏)。FILE* stream

The number of bytes read will be equal to the number of items successfully readtimes the provided sizeof the items.

读取的字节数将等于成功读取的项目数乘以提供的项目大小

Consider the following program.

考虑以下程序。

#include <stdio.h>

int main() {
    char buf[8];
    size_t ret = fread(buf, sizeof(*buf), sizeof(buf)/sizeof(*buf), stdin);
    printf("read %zu bytes\n", ret*sizeof(*buf));
    return 0;
}

When we run this program, depending on the amount of input provided, different outcomes can be observed.

当我们运行这个程序时,根据提供的输入量,可以观察到不同的结果。

We can provide no input at all. The IO stream will be empty to begin with (EOF). The return value will be zero. No items have been read. Zero is returned.

我们根本无法提供任何输入。IO 流将以 (EOF) 开头为空。返回值将为零。未读取任何项目。返回零。

$ : | ./a.out
read 0 bytes

We provide fewer input as requested. Some items will be read before EOF is encountered. The number of items read is returned. No more items are available. Thereafter the stream is empty.

我们根据要求提供较少的输入。有些项目会在遇到 EOF 之前被读取。返回读取的项目数。没有更多可用的项目。此后流为空。

$ echo "Hello" | ./a.out
read 6 bytes

We provide equal or more input as requested. The number of items as requested will be returned. More items may be available.

我们根据要求提供相同或更多的输入。将返回请求的项目数。可能会有更多项目可用。

$ echo "Hello World" | ./a.out
read 8 bytes

Related reading:

相关阅读:

When there are less bytes in the stream than consitute an item, the number of bytes consumedfrom the stream might however be greater than the number of bytes readas calculated by above formula. This answer to above linked question (and the comment to it) I find especially insightful in this matter:

当流中的字节数少于构成项目的字节数时,从流中消耗的字节数可能会大于通过上述公式计算的读取的字节数。这个对上述链接问题的回答(及其评论)我发现在这件事上特别有见地:

回答by Sourav Ghosh

The syntax for fread()is

的语法fread()

size_t fread(void *ptr, size_t size, size_t nmemb, FILE * stream );

which means,

意思是,

The function fread()reads nmembelements of data, each sizebytes long,from the stream pointed to by stream, storing them at the location given by ptr.

该函数从 指向的流中fread()读取nmemb每个size字节长的数据元素stream,并将它们存储在由 给定的位置ptr

So, the total number of bytes read will be nmemb * size.

因此,读取的总字节数将为nmemb * size.

Now, by saying

现在,通过说

on success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1.

成功时, fread() 和 fwrite() 返回读取或写入的项目数。此数字等于仅当 size 为 1 时传输的字节数。

it means that, the return value will equal the nmembwhen the sizeis 1.

这意味着,返回值将等于nmembsize1.

Logic is same, in case of fwrite()also.

逻辑是一样的,在fwrite()也的情况下。



EDIT

编辑

for example, a fully successful call to fread()like

例如,一个完全成功的呼叫fread()喜欢

 fread(readbuf, sizeof(int), 5 , stdin);

will return 5while it will read sizeof(int) * 5bytes. If we assume sizeof(int)is 4, then total bytes read will be 5 * 4or 20. As you can see, here, the number of items read or writtenis not equalto the number of bytes transferred.

5在读取sizeof(int) * 5字节时返回。如果我们假设sizeof(int)4,那么读取的总字节数将为5 * 420。正如你所看到的,在这里,项目的数量读取或写入不相等传输的字节数

OTOH, another fully successful call to fread()like

OTOH,另一个完全成功的呼吁fread()喜欢

fread(readbuf, sizeof(char), 5 , stdin);

will also return 5while it will read sizeof(char) * 5bytes, i.e., 5bytes. In this case, as sizeof(char)is 1, so, here, the number of items read or writtenis equalto the number of bytes transferred. i.e., 5.

也将返回,5同时它将读取sizeof(char) * 5字节,即5字节。在这种情况下,作为sizeof(char)1,因此,在这里,项目数读取或写入就是等于所述传输的字节数。即,5