Understanding fread() C Function for Binary File Reading

  • Thread starter Adyssa
  • Start date
  • Tags
    Function
In summary, the conversation discusses the function fread(), its parameters, and how it reads data from a file. The caller had initially used the function with the size and nmemb arguments reversed, resulting in unexpected results. The expert points out the mistake and clarifies that the return value of fread is the number of records, not the number of bytes.
  • #1
Adyssa
203
3
I'm reading a binary file with fread, and I'm getting some strange results. This is from the man page:

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

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


I understand it to read (nmemb * size) bytes from the file and I'm trying to use it like this:

fread(record_array, num_records, sizeof(record), myfile)

and I'm expecting to read (num_records * sizeof(record)) bytes, call it 10 * 50 = 500.

However, when I do this, it only reads sizeof(record) (50) bytes. If I call it like this it works fine (many elements of size 1 byte each):

fread(record_array, 1, (num_records * sizeof(record)), myfile)

so it seems to ignore the 2nd function parameter. Weird? It's not killing me, but I'm not sure why the size_t size variable is redundant?
 
Technology news on Phys.org
  • #2
You are calling fread with the size and nmemb arguments reversed. Your call should be fread (record_array, sizeof(record), num_records, myfile).

The return value from fread is the number of records that were read, not the number of bytes.
 
  • #3
Oooh I missed that, thanks. Also, they were reversed /facepalm. =S

It's been a long day! :)
 

1. What is the purpose of the fread() function in C?

The fread() function in C is used for reading data from a binary file. It takes in four parameters: a pointer to the data that will store the read data, the size of each element to be read, the number of elements to be read, and a pointer to the binary file to be read from.

2. How does fread() differ from other file reading functions in C?

Fread() is specifically designed for reading binary files, which contain data in its raw form. Other file reading functions like fscanf() and fgets() are used for reading text files, which store data as human-readable characters.

3. What happens if the number of elements to be read exceeds the actual number of elements in the file?

If the number of elements to be read exceeds the actual number of elements in the file, fread() will stop reading once it reaches the end of the file. It will return the total number of elements successfully read, which may be less than the specified number of elements.

4. How does fread() handle errors during reading?

If an error occurs during reading, fread() will return a value less than the specified number of elements to be read. It is important to check the return value of fread() to handle errors appropriately.

5. Can fread() be used to read data from a text file?

While fread() can technically be used to read data from a text file, it is not recommended as it will read the data in its raw form and may cause issues with special characters and line breaks. It is better to use other file reading functions like fscanf() or fgets() for reading text files.

Similar threads

  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
31
Views
2K
Replies
10
Views
956
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
5
Views
6K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top