How to read data from a file using the fread() function in C
The fread() function
In C, the fread() function is used to read data as byte streams from a file and store it in a buffer.
Syntax
size_t fread(void * buffer, size_t size, size_t count, FILE * stream)
Parameters
The fread() takes in four parameters. Let’s look at each of them in detail:
buffer: This is the pointer to the buffer where data will be stored. A buffer is a region of memory used to store data temporarily.size: This is the size (in bytes) of each character to be read.count: This is the total number of characters (each with a sizesize) to be read from the file.stream: This is the pointer to the FILE object from where the data is to be read.
Return value
Upon successful execution, this function returns an integer value, which denotes the number of elements that were actually read from the file. If an error occurred while the file was being read, or an EOF (end of file) was encountered, this value will be less than the parameter count that was passed to the fread() function.
Examples
Let’s take a look at some examples to see how the fread() function works.
Read a string from a file
A single-line string can be read from a file as follows:
#include<stdio.h>int main() {char buffer[20]; // Buffer to store dataFILE * stream;stream = fopen("file.txt", "r");int count = fread(&buffer, sizeof(char), 20, stream);fclose(stream);// Printing data to check validityprintf("Data read from file: %s \n", buffer);printf("Total number of elements read: %d", count);return 0;}
Explanation
We pass the argument to the
sizeparameter issizeof(char). This means that the size (in bytes) of each element to be read from the file is the same as the size of a singlechar, which isByte. Thus, each character is read individually.
Read an integer value from a file
Using fread() to read data into an int variable isn't as simple, and this is demonstrated by the example below:
1234
Explanation
It can be seen above that even though file.txt contained the sequence of characters 1234, reading it from the file into an int leads to an unexpected output of 875770417. This is because when reading data from a file, the fread() function assumes that it is in a binary format (the fread() function reads a byte stream in binary format), whereas our file was written in a text format. This function works fine when reading file content into a char (or char array) though, because characters are stored and read as their ASCII value.
When we need to read integers from a text file, we can use the fgets() or fscanf() functions instead.
Read multiple values from a file
If we wish to read data from a file that contains multiple rows of data, we can proceed as follows:
#include<stdio.h>int main() {char buffer[50]; // Buffer to store dataFILE * stream;stream = fopen("file.txt", "r");int count = fread(&buffer, sizeof(char), 30, stream);fclose(stream);// Printing data to check validityprintf("Data read from file: \n\%s \n", buffer);printf("Elements read: %d", count);return 0;}
Explanation
The fread() function treats the \n character as a single character too, and thus a total of 27 characters are read.
Free Resources