C语言 C 编程中的 fread 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7766842/
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 09:54:02  来源:igfitidea点击:

fread Function in C Programming

cfread

提问by mainajaved

I have two questions about C's freadfunction:

我有两个关于 Cfread函数的问题:

  1. I have read that freadis used to read a binary file. However, when I read a binary file with fgetsusing read mode "r"and a text file with freadusing "rb"mode, the results are the same as reading a text file with fgetsand a binary file with fread. So, why are there different functions for reading binary and text files?

  2. I am using freadto read 10 bytes of a file in one call. How should I stop reading at the end of file – i.e. how is EOFspecified in fread?

  1. 我读过fread用于读取二进制文件的内容。然而,当我读到一个二进制文件fgets使用阅读模式"r"和一个文本文件中fread使用"rb"模式,结果是一样的阅读与文本文件fgets和一个二进制文件fread。那么,为什么读取二进制文件和文本文件有不同的函数呢?

  2. 我正在使用fread一次调用读取文件的 10 个字节。我应该如何在文件末尾停止阅读——即如何在 中EOF指定fread

回答by Jeegar Patel

answer of 1 question >

回答 1 个问题 >

1>fread

1>恐惧

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

Read block of data from stream(try to understand this)

从流中读取数据块(尝试理解这一点)

Reads an array of count elements, each one with a size of size bytes, from the stream and stores them in the block of memory specified by ptr. The postion indicator of the stream is advanced by the total amount of bytes read. The total amount of bytes read if successful is (size * count).

从流中读取 count 个元素的数组,每个元素的大小为 size 字节,并将它们存储在 ptr 指定的内存块中。流的位置指示符提前读取的总字节数。如果成功则读取的总字节数为(大小 * 计数)。

2>fgets

2>fgets

char * fgets ( char * str, int num, FILE * stream );

Get string from stream(try to understand this)

从流中获取字符串(尝试理解这一点)

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or a the End-of-File is reached, whichever comes first. A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str. A null character is automatically appended in str after the characters read to signal the end of the C string.

从流中读取字符并将它们作为 C 字符串存储到 str 中,直到读取 (num-1) 个字符或到达换行符或文件结尾,以先到者为准。换行符使 fgets 停止读取,但它被视为有效字符,因此它包含在复制到 str 的字符串中。在读取字符后,一个空字符会自动附加到 str 中,以表示 C 字符串的结束。



answer of 2nd questionin fread return value is

fread 返回值中第二个问题的答案

The total number of elements successfully read is returned as a size_t object, which is an integral data type. If this number differs from the count parameter, either an error occured or the End Of File was reached.

成功读取的元素总数作为 size_t 对象返回,这是一个整数数据类型。 如果此数字与计数参数不同,则发生错误或到达文件结尾。

You can use either ferror or feof to check whether an error happened or the End-of-File was reached.

您可以使用 ferror 或 feof 来检查是否发生了错误或是否到达文件尾。

回答by Paul R

When you talk about "r" and "rb" modes (text and binary) you are probably referring to fopen. On most operating systems it makes no difference whether you open a file in binary mode or text mode, but on some operating systems there are translations which need to occur in text mode that are disabled in binary mode. An example of this is when running under DOS or Windows, where end of line character conversion takes place in text mode, but not in binary mode.

当您谈论“r”和“rb”模式(文本和二进制)时,您可能指的是fopen。在大多数操作系统上,以二进制模式或文本模式打开文件没有区别,但在某些操作系统上,需要在文本模式下进行的转换在二进制模式下被禁用。一个例子是在 DOS 或 Windows 下运行时,行尾字符转换发生在文本模式下,而不是在二进制模式下。

It's a good idea to get into the habit of using "rb" with fopenfor binary files, even though it has no effect on most platforms - it's always possible that at some point in the future your code may need to run on an OS where this matters.

养成fopen对二进制文件使用“rb”的习惯是一个好主意,即使它对大多数平台没有影响 - 在未来的某个时候,您的代码可能需要在操作系统上运行,这总是有可能的事项。

回答by theartist33

If we're doing binary I/O, we often would like to read or write an entire structure at a time. To do this using getc or putc, we have to loop through the entire structure, one byte at a time, reading or writing each byte. We can't use the line-at-a-time functions, since fputs stops writing when it hits a null byte, and there might be null bytes within the structure. Similarly, fgets won't work right on input if any of the data bytes are nulls or newlines, hence fread and fwrite is provided.

如果我们在进行二进制 I/O,我们通常希望一次读取或写入整个结构。要使用 getc 或 putc 做到这一点,我们必须遍历整个结构,一次一个字节,读取或写入每个字节。我们不能使用 line-at-a-time 函数,因为 fputs 在遇到空字节时停止写入,并且结构中可能有空字节。类似地,如果任何数据字节为空或换行符,fgets 将无法在输入上正常工作,因此提供了 fread 和 fwrite。