Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is fread C?
2.1.
Syntax
2.2.
Parameters of fread C
2.3.
fread C Returns
3.
Examples 
3.1.
Reading Data from a Text File
3.1.1.
Output
3.1.2.
Explanation
3.2.
Reading Data From a Binary File
3.2.1.
Output
3.2.2.
Explanation
4.
Frequently Asked Questions
4.1.
What is fread C?
4.2.
What fread C function return?
4.3.
How is fread C different from fgets()?
4.4.
What happens if fread C encounters an error while reading a file?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

fread C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hello Ninjas, welcome to this blog on the topic fread C. Have you ever thought about why to use fread C? If not, and you want to know about it, then don't worry, ninjas. Coding Ninjas got your back. We will clear all your doubts.

fread C

In this article, we will discuss about fread C. We will discuss why it is used in C programming. We will also understand some examples by using this function. So, it is a standard library function in the C programming language. This is used to read a block of data from a file. Let us understand more about fread C along with its syntax.

Also see: C Static Function, and  Tribonacci Series

What is fread C?

fread C is a standard library function that is used to read a block of data from a file. The fread() function in C reads count elements of data from the file, each size bytes long. This function is mostly used for reading binary data from a given file. This data can be arrays of structures, or it can be other complex data types. This function also can be used to read any type of data. Let us understand the syntax of it.

Syntax

The syntax of fread C is given below:

size_t fread(void *ptr, size_t size, size_t count, FILE *stream);

 

Let us try to break down this syntax and understand what parameters we must use in fread C.

Parameters of fread C

There are different types of parameters in fread() function in C:

  • ptr is a void-type pointer that points to the block of memory. This block of memory is a place where our data will be stored.
     
  • size indicates the size in bytes of each element that needed to be read. 
     
  • count indicates the number of elements to read.
     
  • stream is a pointer to the FILE object representing the file to read from.
     

Now you might be thinking about what this function returns. Let us understand about this.

fread C Returns

When we execute this function, it will return a number. This number indicates the number of items successfully read. While executing this function, if an error comes, then it will return a number. This number will be less than the number of items requested to read.

Let us understand fread C with some examples.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Examples 

Here are some examples by using the fread C. 

Reading Data from a Text File

In this example, we will understand how fread C function works while reading a text file. There are some steps that we need to do before writing the code.

Step 1: We have to create a file. We have created a file with the name ninjas.txt

creating text file

Step 2: Then we can write something in it manually or by using fwrite() function. So, we have written this:

Hello Ninjas, How are you doing today?
This is a fread C example.

 

Step 3: Now, we will write our code for reading this file by using fread C.

#include <stdio.h>
int main() {
     // Creating a buffer
    char buffer[100];

    // Opening the file ninjas.txt
    FILE *file = fopen("ninjas.txt", "r");

    // If file is not created
    if (file == NULL) {
        printf("Oops! File not found.");
        return 1;
    }

    // If a file is there, then fread C will work
    while(fread(buffer, sizeof(char), sizeof(buffer), file) != 0) {
        printf("%s", buffer);
    }

     // Closing the file
    fclose(file);
    return 0;
}

Output

output

Explanation

In this example, we have discussed a program in C that uses fread C to read the data from the file ninjas.txt. This file contains a string of characters. That’s why we have created a buffer of 100 characters to store this data. Then we have used a function to open the file, i.e., fopen(). We have opened this file in read mode by providing the ‘r’ in fopen() function. Then we have created a while loop that reads the data from the file by using the fread C.

Reading Data From a Binary File

In this example, we will understand how the fread C function works while reading a binary file. There are some steps that we need to do before writing the code.

Step 1: We have to create a binary file. We have created a file with the name ninjasdetails.bin.

creating a binary file

Step 2: Then we have to write something in it manually or by using fwrite() function. So, we have written this:

Narayan 20 400

 

Step 3: Now, we will write our code for reading this binary file by using fread C.

#include <stdio.h>

// Ninja Details
struct Ninja {
    char name[20];
    int age;
    int questionsSolved;
};

int main() {
    struct Ninja ninja;
    // Opening the ninjasdetails.bin file
    FILE *file = fopen("ninjasdetails.bin", "rb");
   // If file not found
    if (file == NULL) {
        printf("Oops! File not found.");
        return 1;
    }

    // If file found then start reading
    while(fread(&ninja, sizeof(struct Ninja), 1, file) != 0) {
        printf("Name: %s\nAge: %d\nQuestions Solved: %d\n\n", ninja.name, ninja.age, ninja.questionsSolved);
    }

    // Closing the file
    fclose(file);
    return 0;
}

Output

output

Explanation

In this example, we have discussed a program in C that uses fread C to read the data from the binary file ninjasdetails.bin. This file contains details of ninjas, such as name, age, and the number of questions they have solved. That’s why we have created a structure Ninja to store this data. Then we have used a function to open the file, i.e., fopen(). We have opened this file in read-in binary mode by providing the ‘rb’ in fopen() function. Then we created a while loop that reads the data from the binary file by using the fread C.

Also see, Short int in C Programming

Frequently Asked Questions

What is fread C?

fread is a standard library function in C programming. It is used to read a block of data from a file. stdio.h header file provides this method.

What fread C function return?

fread() function consists of four parameters a pointer for a memory block, size, count, and stream. This fread C function returns the number of items that are read successfully from a file. 

How is fread C different from fgets()?

fread C and fgets() are both standard library functions in C. fread C is used to read binary data from a file. On the other hand, fgets() is used to read text data from a file. 

What happens if fread C encounters an error while reading a file?

While reading, if an error occurs, this will return a number that will be less than the number of items requested to read, or else it will return 0.

Conclusion

In this article, we have discussed the fread C. We have also discussed examples by using this function. You can check out our other blogs to enhance your knowledge:

We hope this blog helped you to understand the concept of fread C. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews

Happy Learning!!

Previous article
fgets() and gets() in C
Next article
fwrite() Function in C
Live masterclass