The C fread() is a standard library function used to read the given amount of data from a file stream. Defined inside <stdio.h>, the fread() function reads the given number of elements of specific size from the file stream and stores it in the buffer memory. The total number of bytes read by fread() function is the number of elements read multiplied by the size of each element in bytes.
Syntax of C fread()
size_t fread(void * buffer, size_t size, size_t count, FILE * stream);
The file position indicator is automatically moved forward by the number of bytes read. If the objects being read are not trivially copy-able, such as structures or complex data types then it does not behave properly.
Parameters
- buffer: It refers to the pointer to the buffer memory block where the data read will be stored.
- size: It refers to the size of each element in bytes.
- count: It refers to the count of elements to be read.
- stream: It refers to the pointer to the file stream.
Return Value
- The function returns the number of elements that are read successfully from the file.
- If the return value is less than the count, it means that an error occurred or it has reached the end of the file.
- If the value of size or count is zero, fread() returns zero and performs no other action.
Note: fread() function itself does not provide a way to distinguish between end-of-file and error, feof and ferror can be used to determine which occurred.
Examples of C fread()
Example 1
The below programs illustrate the fread() function.
C
#include <stdio.h>
int main()
{
FILE * filePointer;
char buffer[100];
filePointer = fopen ( "g4g.txt" , "r" );
while (! feof (filePointer)) {
fread (buffer, sizeof (buffer), 1, filePointer);
printf ( "%s" , buffer);
}
fclose (filePointer);
return 0;
}
|
Suppose the file g4g.txt contains the following data:
Geeks : DS-ALgo
Gfg : DP
Contribute : writearticle
Then, after running the program, the output will be
Geeks : DS-ALgo
Gfg : DP
Contribute : writearticle
Example 2
This C program demonstrates the usage of the fread() function when the file’s size or count is equal to 0.
C
#include <stdio.h>
int main()
{
FILE * filePointer;
char buffer[100];
filePointer = fopen ( "g4g.txt" , "r" );
printf ( "count = 0, return value = %zu\n" ,
fread (buffer, sizeof (buffer), 0, filePointer));
printf ( "size = 0, return value = %zu\n" ,
fread (buffer, 0, 1, filePointer));
return 0;
}
|
Output
count = 0, return value = 0
size = 0, return value = 0
C++
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< int > v1 = { 1, 2, 3, 4, 5 };
vector<vector< int > > v2(3, vector< int >(3, 5));
v1.push_back(6);
cout << "v1: " ;
for ( int i = 0; i < v1.size(); i++) {
cout << v1[i] << " " ;
}
cout << endl;
v1.erase(v1.begin() + 4);
cout << "v2: " ;
for ( auto i = v1.begin(); i != v1.end(); i++) {
cout << *i << " " ;
}
cout << endl;
cout << "v2:-" << endl;
for ( auto i : v2) {
for ( auto j : i) {
cout << j << " " ;
}
cout << endl;
}
return 0;
}
|
Output
v1: 1 2 3 4 5 6
v2: 1 2 3 4 6
v2:-
5 5 5
5 5 5
5 5 5
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jun, 2023
Like Article
Save Article