简体   繁体   English

C编程中的fread功能

[英]fread Function in C Programming

I have two questions about C's fread function: 我有两个关于C的fread函数的问题:

  1. I have read that fread is used to read a binary file. 我已经读过fread用于读取二进制文件。 However, when I read a binary file with fgets using read mode "r" and a text file with fread using "rb" mode, the results are the same as reading a text file with fgets and a binary file with fread . 但是,当我使用读取模式"r"读取带有fgets的二进制文件和使用"rb"模式使用fread读取文本文件时,结果与使用fgets读取文本文件和使用fread读取二进制文件相同。 So, why are there different functions for reading binary and text files? 那么,为什么有不同的函数来读取二进制文件和文本文件?

  2. I am using fread to read 10 bytes of a file in one call. 我正在使用fread在一次调用中读取10个字节的文件。 How should I stop reading at the end of file – ie how is EOF specified in fread ? 我应该如何在文件末尾停止阅读 - 即如何在fread指定EOF

answer of 1 question > 回答1个问题>

1> fread 1> fread

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. 从流中读取一个count元素数组,每个元素的大小为字节大小,并将它们存储在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). 成功读取的总字节数是(size * count)。

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. 从流中读取字符并将它们作为C字符串存储到str中,直到读取(num-1)个字符或者到达换行符或文件结尾,以先到者为准。 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. 换行符使fgets停止读取,但它被认为是有效字符,因此它包含在复制到str的字符串中。 A null character is automatically appended in str after the characters read to signal the end of the C string. 在读取字符后,空字符会自动附加在str中,以表示C字符串的结尾。


answer of 2nd question in 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. 成功读取的元素总数将作为size_t对象返回,该对象是一种完整的数据类型。 If this number differs from the count parameter, either an error occured or the End Of File was reached. 如果此数字与count参数不同,则会发生错误或达到文件结尾。

You can use either ferror or feof to check whether an error happened or the End-of-File was reached. 您可以使用ferror或feof来检查是否发生了错误或是否已达到文件结尾。

When you talk about "r" and "rb" modes (text and binary) you are probably referring to fopen . 当你谈到“r”和“rb”模式(文本和二进制)时,你可能指的是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. 例如,在DOS或Windows下运行时,行结束字符转换在文本模式下进行,而不是在二进制模式下进行。

It's a good idea to get into the habit of using "rb" with fopen for 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. 最好养成使用“rb”和fopen进行二进制文件的习惯,即使它对大多数平台没有影响 - 总有可能在未来的某个时候你的代码可能需要在操作系统上运行这很重要。

If we're doing binary I/O, we often would like to read or write an entire structure at a time. 如果我们正在进行二进制I / O,我们通常希望一次读取或写入整个结构。 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. 要使用getc或putc执行此操作,我们必须遍历整个结构,一次一个字节,读取或写入每个字节。 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. 我们不能使用一次一行的函数,因为fput在遇到空字节时停止写入,并且结构中可能有空字节。 Similarly, fgets won't work right on input if any of the data bytes are nulls or newlines, hence fread and fwrite is provided. 类似地,如果任何数据字节是空值或换行符,则fgets将无法正确输入,因此提供了fread和fwrite。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM