Fread in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The fread in C reads data from the file and stores it in a buffer. The fread function reads up to count objects into an array buffer from the input stream passed in the function argument. The file position indicator for the given input stream advances by the number of character function reads.

Syntax of fread() Function in C

The syntax of fread() function in C is as follows:

Parameters of fread() Function in C

The fread in C takes in several parameters. Let's look at these parameters individually in detail:

  • array_buffer: A buffer is a space in a computer's memory that is used to store data temporarily. This parameter is a pointer pointing to the memory location of the buffer where data read from the input stream will be stored.
  • size: This parameter tells the function the size of each block in bytes to be read from the input stream.
  • count: The count parameters define the number of characters that will be read from the input stream of data.
  • file_stream: It is a pointer pointing to a FILE object from which data will be read and later stored in the location pointed by arraybuffer.

Return value of fread() Function in C

When the function call executes successfully, the fread function in C returns an integer value that signifies the number of elements read (count). If an error occurs or EOF (the value of EOF is -1 and is used to indicate the end of input) is encountered, the fread function in C returns an integer value with a value less than count.

Example

Now that we are familiar with the syntax of the fread in C let us look at an example to see how the fread() function is used to read input from a file.

*Output

Here, once we opened a file from the computer. We pass a pointer pointing to the FILE object buffer to the fread function. Because we are reading characters from the file stream, we pass size as sizeof(char), and integer value 33 is passed to the function that specifies we wish to read 33 characters from the input file stream.

What is fread() function in C?

fread-function-in-c

As shown in the above image, the fread in C is complementary to the fwrite function in C. The fread() function reads a block of data from the input file stream. The function reads count number of elements from the given input stream, each with size bytes. Upon successful execution, the function returns an integer value equal to count, or else a value less than count is returned. The file position indicator for the given input stream advances by the number of character function reads.

More Examples

Reading an integer value from a file

Now we know how the fread in C can be used to read a string from a file. Let us see an example where we read an integer value from the file.

Output

In the above example, to read an integer value from the file, the only change we need to do is to replace the value of size with sizeof(int) instead of sizeof(char) because we are reading integer values. Also, the value 1 passed to the function signifies that we wish to read one integer value from the file.

Reading Multiple Values from a File

The fread in C can be used to read data from the file that contains data in multiple rows. This is explained in the program as follows:

Output

Here, we returned the integer value 27 from the function because the file content is only 27 characters long.

Conclusion

  • The fread in C is a function that reads data from a file and stores it in a buffer.
  • The function reads count number of elements from the given input stream, each with size bytes.
  • The fread() function in C accepts four parameter values.
  • Upon successful execution, the function returns an integer value equal to count, or else a value less than count is returned.