The `fread()` function in C is used for reading binary data from a file. It reads a specified number of elements, each of a given size, from an input stream and stores them in an array. The function takes four arguments: a pointer to the array where the data will be stored, the size of each element, the number of elements to read, and a pointer to the FILE object. `fread()` returns the total number of elements successfully read, which may be less than the requested amount. In this article, we will discuss about fread C. We will discuss why it is used in C programming.
fread() in 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.
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.
Return Value of fread() function in C
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.
Examples of C fread() function
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.
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;
}
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’ infopen() 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.
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;
}
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.
fread is used to read raw binary data from a stream, while fscanf reads formatted input, parsing it according to specified format specifiers.
What is the difference between read and fread in C?
read is a system call that performs low-level I/O operations, directly interfacing with the OS. fread is a higher-level function that reads data from a file stream, handling buffering and type conversion.
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 learned about the `fread()` function in C, which is a powerful tool for reading binary data from files. By specifying the size and number of elements to read, `fread()` efficiently populates an array with data from an input stream. The knowledge of `fread()` is very important for every programmer who is working with file I/O and binary data manipulation in C, at last, we saw different examples of using this function.