Javatpoint Logo
Javatpoint Logo

Fread() Function in C

In this article, we will discuss the fread() function in C with its syntax and examples.

The C fread() function is used to read the data from the file and store it in a buffer. The fread() function reads from the input stream supplied as the function argument up to count objects into an array buffer. The number of character functions that read the data causes advancement in the file position indication for the specified input stream.

Syntax of fread() function in C:

The following is the syntax for the C method fread():

size_tfread(void * array_buffer, size_t size, size_t count, FILE * file_strea)

fread() function's parameters in the C language

Numerous parameters are input into the fread() function. Let's take a closer look at each of these variables separately:

array_buffer:

It is possible to temporarily store data in a computer's memory by using a buffer. Data read from the input stream will be saved in the buffer at the address indicated by this argument, which is a pointer to the buffer's memory location.

size:

The size of each block in bytes to be read from the input stream is specified by this parameter to the function.

count:

The character count that will be read from the input stream of data is determined by the count parameters.

file_stream:

It is a pointer pointing to a FILE object from which data will be read and subsequently placed at the address indicated by arraybuffer.

Value returned by the fread() function in C

When a function call is properly completed, the fread() function in C returns an integer value that represents the total number of elements read (count). The fread() function returns an integer value with a value smaller than count if there is no error or if EOF (which has the value -1 and indicates the end of input) is encountered.

Example:

Let's look at an example to see how the fread() method is used to read input from a file.

Output:

Data read from the file: Hello, World! This is a test.
Number of elements read: 31

What does the fread() function do?

The fread() function in C is an addition to the fwrite() function. A block of data is read from the input file stream by the fread() function. The function reads count elements, each with a size of bytes, from the specified input stream. The function either returns a number less than count or an integer value equal to count upon successful operation. The number of character functions that read the content causes advancement in the file position indication for the specified input stream.

Additional Cases:

There are several additional cases in the fread() function in C. Some of them are as follows:

reading a number as an integer from a file:

Now, let's take a program to understand how we can use the fread() function in C to read a string from a file. As an illustration, let's read an integer value from the file.

Output:

Integer read from the file: 42

Explanation:

  • In this example, an integer value is read by the program from the binary file "integer_data.bin".
  • After that, the integer read from the file, 42, is printed on the screen.

Multiple Values Reading from a File:

Use C's fread() function to read data from a file with several rows of data.

Example:

Now, let's take a program to understand how we can use the fread() function in C to read multiple values from a file.

Output:

Text read from the file:
This is a sample text file.
Number of elements read: 27

Exaplanation:

  • In this example, the text data is read by the program from the text file "text_data.txt".
  • The content of the text file is presented as the phrase "This is a sample text file".
  • 27 characters (including spaces) were successfully read from the file and placed in the buffer, as indicated by the "Number of elements read" value of 27.

Conclusion:

  • The fread() function is used to read data from files and put it in a buffer.
  • The function reads count elements, each with a size of bytes, from the specified input stream.
  • There are four possible parameter values for the fread() function.
  • The function either returns a number less than count or an integer value equal to count upon successful operation.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA