The fread() function in C++ reads the block of data from stream. This function first, reads count number of objects, each one with a size of size bytes from the given input stream.
The total amount of bytes read, if successful is ( size*count ). According to the no. of characters read, the indicator file position is incremented. If the objects read are not trivially copy-able, then the behavior is undefined and if the value of size or count is equal to zero, then this program will simply return 0.
size_t fread(void * buffer, size_t size, size_t count, FILE * stream)
Parameter : The function accepts four mandatory parameters which are described as below:
- buffer: it specifies the pointer to the block of memory with a size of at least (size*count) bytes to store the objects.
- size: it specifies the size of each objects in bytes. size_t is an unsigned integral type.
- count: it specifies the number of elements, each one with a size of size bytes.
- stream: it specifies the file stream to read the data from.
Return Value: The function returns the number of objects read successfully. If an error occurs, the return value can be less than count.
Below programs illustrate the above function:
Suppose the file g4g.txt contains following data:
Geeks : DS-ALgo Gfg : DP Contribute : writearticle
Then, when you will run the program, the output will be
Harry Potter : Specs Hermione : Smart Weasley : FlyingCar Dumbledore : Wand
Program 2 :
count = 0, return value = 0 size = 0, return value = 0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call some function before main() function in C++?
- How to call function within function in C or C++
- Difference between user defined function and library function in C/C++
- Difference between virtual function and inline function in C++
- Can we call an undeclared function in C++?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers
- Function overloading and return type
- Function Pointer in C
- Default arguments and virtual function
- Function overloading and const keyword
- How to measure time taken by a function in C?
- Comparator function of qsort() in C
- Can we access private data members of a class without using a member or a friend function?
- Function Overloading and float in C++
- Extra brackets with function names in C/C++
- Function Interposition in C with an example of user defined malloc()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.