The fread() function is a standard library function declared in the <stdio.h> header file. It is used to read binary data from a file and store it in a memory buffer.
- It reads multiple elements from a file based on the specified element size and count.
- It is commonly used to read arrays, structures, and other binary data from files.
- On success, it returns the number of elements successfully read; if the return value is less than the requested count, it indicates an error or end-of-file.
#include <stdio.h>
int main() {
FILE *file;
int buffer[5];
// Open the binary file for reading
file = fopen("input.bin", "rb");
if (file == NULL) {
perror("Error opening file");
return 1;
}
// Read the integers from the file into the buffer
fread(buffer, sizeof(int),5, file);
// Print the integers that were read
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i + 1, buffer[i]);
}
// Close the file
fclose(file);
return 0;
}
Output
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
Here, input.bin file should contain the binary representation of the integers: 10, 20, 30, 40, 50.
Syntax
size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
Parameters
- buffer: Pointer to the memory block where the read data will be stored.
- size: Size of each element to be read, in bytes.
- count: Number of elements to read from the file.
- stream: Pointer to the file from which the data will be read.
Return Value
- Returns the number of elements successfully read from the file.
- If the return value is less than count, it indicates that an error occurred or the end of the file was reached.
- If size or count is zero, the function returns 0 without reading any data.
Working
The fread() function reads a specified number of elements from a file and stores them in the provided memory buffer.
- Open the file using fopen() in read ("r" or "rb") mode.
- Pass the buffer, element size, element count, and file pointer to fread().
- The function reads the data and stores it in the buffer.
- After reading, close the file using fclose().
Note: The fread() function cannot distinguish between an end-of-file condition and a read error on its own. Use the feof() function to check for end-of-file and ferror() to detect file read errors.
Advantages of fread()
The fread() function provides an efficient way to read data from files, especially binary files.
- Reads multiple elements from a file in a single function call.
- Efficient for reading arrays, structures, and other binary data.
- Supports reading any data type by specifying the element size and count.
- Automatically advances the file position after each read operation.
Limitations of fread()
Although fread() is useful for reading binary data, it has a few limitations.
- It cannot distinguish between end-of-file and a read error on its own.
- It is primarily intended for binary files and may not be suitable for formatted text input.
- Reading structures directly may cause portability issues due to compiler-specific padding and alignment.
- The programmer must allocate a buffer of sufficient size before calling fread().