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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.