C fread() Function
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
// C program to illustrate fread() function #include <stdio.h> int main() { // File pointer FILE * filePointer; // Buffer to store the read data char buffer[100]; // "g4g.txt" file is opened in read mode filePointer = fopen ( "g4g.txt" , "r" ); // Data is read from the file into the buffer // sizeof(buffer) specifies the size of each element to // be read 1 is the number of elements to read // filePointer is the file to read from while (! feof (filePointer)) { fread (buffer, sizeof (buffer), 1, filePointer); // Print the read data printf ( "%s" , buffer); } fclose (filePointer); return 0; } |
Suppose the file g4g.txt contains the following data:
Beginner : DS-ALgo Gfg : DP Contribute : writearticle
Then, after running the program, the output will be
Beginner : 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
// C program to illustrate fread() function // when size of the file or the value of count is equal to 0 #include <stdio.h> int main() { // File pointer FILE * filePointer; // Buffer to store the read data char buffer[100]; // "g4g.txt" file is opened in read mode filePointer = fopen ( "g4g.txt" , "r" ); // Case when count is equal to 0 printf ( "count = 0, return value = %zu\n" , fread (buffer, sizeof (buffer), 0, filePointer)); // Case when size is equal to 0 printf ( "size = 0, return value = %zu\n" , fread (buffer, 0, 1, filePointer)); return 0; } |
count = 0, return value = 0 size = 0, return value = 0
C++
// C++ program to illustrate the vector container #include <iostream> #include <vector> using namespace std; int main() { // 1d vector with initialization list vector< int > v1 = { 1, 2, 3, 4, 5 }; // 2d vector with size and element value initialization vector<vector< int > > v2(3, vector< int >(3, 5)); // adding values using push_back() v1.push_back(6); // printing v1 using size() cout << "v1: " ; for ( int i = 0; i < v1.size(); i++) { cout << v1[i] << " " ; } cout << endl; // deleting value usign erase and iterators v1.erase(v1.begin() + 4); // printing v1 using iterators cout << "v2: " ; for ( auto i = v1.begin(); i != v1.end(); i++) { cout << *i << " " ; } cout << endl; // printing v2 using range based for loop cout << "v2:-" << endl; for ( auto i : v2) { for ( auto j : i) { cout << j << " " ; } cout << endl; } return 0; } |
v1: 1 2 3 4 5 6 v2: 1 2 3 4 6 v2:- 5 5 5 5 5 5 5 5 5
Contact Us