Syntax of fread function in C:
The syntax below illustrates how to use the C function fread:
Reads data from a file stream into an array buffer, with the specified size and count.
fread function's parameters in the C language
Several arguments are passed to the fread function. Now, we will examine each of these parameters individually:
array_buffer:
Data can be temporarily stored in a computer's memory utilizing a buffer. Information retrieved from the input stream will be stored in the buffer at the specified memory address, which is represented by a pointer pointing to the buffer's memory location.
size:
The parameter passed to the function specifies the size of each block in bytes to be read from the input stream.
count:
The number of characters to be read from the input data stream is specified by the count parameters.
file_stream:
It represents a pointer that directs to a FILE object where data will be read and then stored at the location specified by arraybuffer.
Value returned by the fread function in C
Upon successful execution of a function call, the fread function within the C programming language will provide an integer output denoting the total count of elements that have been read. If no error occurs or if the end of input (EOF) is reached - signified by a value of -1 - fread will return an integer smaller than the specified count parameter.
Example:
Let's examine a demonstration to observe the manner in which the fread function is employed for reading input from a file.
#include <stdio.h>
int main() {
// buffer to store data
char buffer[50]; // Increased buffer size for demonstration
// FILE object
FILE *stream;
// opening a file in binary read mode ("rb" instead of "r")
stream = fopen("file_name.bin", "rb"); // Using a binary file for demonstration
if (stream == NULL) {
perror("Error opening file");
return 1; // Exit with an error code
}
// Reading data from the file
int count = fread(buffer, sizeof(char), sizeof(buffer), stream);
// close the opened file
fclose(stream);
printf("Data read from the file: ");
for (int i = 0; i< count; i++) {
printf("%c", buffer[i]);
}
printf("\nNumber of elements read: %d\n", count);
return 0;
}
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 the C programming language complements the fwrite function. It reads a block of data from the input file stream, retrieving count elements, each with a byte size, from the designated input stream. Upon successful execution, the function returns either a value less than count or an integer equivalent to count. Reading content with character functions triggers the advancement of the file position indicator for the specified input stream.
Additional Cases:
There are various extra scenarios within the fread function in C. A few examples are listed below:
reading a number as an integer from a file:
Now, let's examine a sample code to demonstrate the utilization of the fread function in the C language for reading a string from a file. For instance, we will read a numeric value from the file.
#include <stdio.h>
int main() {
// variable to store data
int value;
// FILE object
FILE *stream;
// opening a binary file in read mode ("rb" instead of "r")
stream = fopen("integer_data.bin", "rb"); // Using a binary file for demonstration
if (stream == NULL) {
perror("Error opening file");
return 1; // Exit with an error code
}
// Reading an integer from the file
int count = fread(&value, sizeof(int), 1, stream);
// close the opened file
fclose(stream);
printf("Integer read from the file: %d\n", value);
return 0;
}
Output:
Integer read from the file: 42
Explanation:
- Within this illustration, the program retrieves an integer value from the binary file named "integer_data.bin".
- Subsequently, the program displays the integer 42 read from the file onto the screen.
Multiple Values Reading from a File:
Utilize the fread function in C to extract information from a file containing multiple rows of data.
Example:
Now, let's explore a program to grasp the utilization of the fread function in the C programming language for reading multiple data values from a file.
#include <stdio.h>
int main() {
// buffer to store data
char text[100]; // Increased buffer size for demonstration
// FILE object
FILE *stream;
// opening a binary file in read mode ("rb" instead of "r")
stream = fopen("text_data.txt", "r"); // Using a text file for demonstration
if (stream == NULL) {
perror("Error opening file");
return 1; // Exit with an error code
}
// Reading text data from the file
int count = fread(text, sizeof(char), sizeof(text), stream);
// close the opened file
fclose(stream);
printf("Text read from the file:\n%s\n", text);
printf("Number of elements read: %d\n", count);
return 0;
}
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 .
- 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.