fread() Function in C++ Programming
C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language; it was developed by Bjarne Stroustrup.
Features of C++
- High-level language.
- Object-oriented programming language
- Portable
- Graphical user interference
- Simple
- Structured programming language
C++ language is somewhat the same as that of other languages like C, Java, and C#.
fread() in C++
In C++ programming, the fread() function reads the data from the bunch along the stream or the characters from the stream. Initially, the function tends to read the count of the number of objects, each having a size of size bytes in the input stream.
fread() Prototype
size_t fread (void * buffer, size_t size, size_t count, FILE * stream);
The above one is the prototype for the fread() function, or it can be called the prototype for the function fread().
There are four parameters listed in that format that are:
- Size: It refers to the size of each object or character and is represented in Bytes.
- Count: Counts the number of objects or characters.
- Stream: used to read data from the stream.
- Buffer: It is used to store the objects or the characters which point to the block of memory.
The behavior is undefinable if the items are not easily duplicable. A call to fread() will return zero and do nothing else if the size or count is zero. If there is a problem while reading the file, the value of the file position indicates the stream is undefined.
fread() return value
Returns the number of objects or characters that are being read successfully. Suppose the return value is less than the count. It throughs an error. The fread() function also does not act if the return value is written as zero.
C++ Program for fread() function
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
FILE *fileopen;
char buff[100];
fileopen = fopen("demo.txt","rb");
while(!feof(fileopen))
{
fread(buff,sizeof(buff),1,fileopen);
cout << buff;
}
return 0;
}
Output
Father Occupation: Business
Mother Occupation: Home Wife
Grand Father Occupation: Farmer
Your Occupation: Student
Note: Your output may vary because you may enter different data, and it will display different results on the terminal of your computer or the system.
Explanation of the above program:
Here we specified rb in the code in the function fopen() to read the code in the binary format after the open. First, you have to create a text file in your computer in a folder to execute the program. From that file, the program which you have written will open and read the file in binary format. Here we used the header file of #include<cstdio>, which imports all the functions along with the fread() function. Here fopen() is used to open the file, and fread() is used to convert the text into binary format and read the text. Cout is used to print the information or the result in the terminal. The text file contains the above data of the output, which was printed as the output.
C++ Program for fread() function:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
FILE *fileopen;
char buff[100];
int printVal;
fileopen = fopen("demo.txt","rb");
printVal = fread(buff,sizeof(buff),0,fileopen);
cout << " If count = 0, return value will be = " << printVal << endl;
printVal = fread(buff,0,1,fileopen);
cout << " If size = 0, return value will be = " << printVal << endl;
return 0;
}
Output:
If count = 0, return value will be = 0
If size = 0, return value will be = 0
Explanation of the above program:
We already discussed that the return value of the program would be zero if the count value and size value were zero. In the above program, we followed a particular syntax that was mentioned in the fread() function prototype.
Conclusion
Hence the function is used to read the text of the file that has been created and print the output in the terminal and reads the number of objects in the input stream. By using the fread() function, we cannot alter the text file, but by the fwrite() function, we can alter the text of the text file that has been created. In creating the fread() function, we have to allocate some memory.