fread Function in C Programming
Asked Answered
A

3

7

I have two questions about C's fread function:

  1. I have read that fread is used to read a binary file. 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. 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. How should I stop reading at the end of file – i.e. how is EOF specified in fread?

Aphorism answered 14/10, 2011 at 11:18 Comment(1)
all files are binary files, it is way you open that determines whether the END OF LINE character should mean something or not.Huldahuldah
H
10

answer of 1 question >

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. 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).

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.


answer of 2nd question in fread return value is

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.

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

Hospice answered 14/10, 2011 at 11:28 Comment(0)
G
2

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.

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.

Glisten answered 14/10, 2011 at 11:28 Comment(0)
F
0

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.

Freshen answered 13/5, 2016 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.