×

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++

  1. High-level language.
  2. Object-oriented programming language
  3. Portable
  4. Graphical user interference
  5. Simple
  6. 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:

  1. Size: It refers to the size of each object or character and is represented in Bytes.
  2. Count: Counts the number of objects or characters.
  3. Stream: used to read data from the stream.
  4. 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.


Related Topics

Compile Time Polymorphism in C++

What is Polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. One application of polymorphism in...

4 minutes read.

Pthread in C++ Parameters

Pthreads, also known as POSIX threads, is a POSIX standard for multithreading in C/C++. It allows a program to control multiple different threads of execution concurrently. Using pthreads, you can create...

4 minutes read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Unary Operators in C++

Unary operators in C++ Unary operator: is operations that function to produce a new value on a single operand. a) unary minus: A minus operator modifies the argument's symbol. A positive number...

3 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Advanced C++ with Boost Library

The goal of the Boost Libraries is to be widely applicable and used in a variety of applications. For instance, they can in handy when working with huge numbers whose...

4 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.