fread
Header: <stdio.h>
Reads up to count objects into the array buffer from the given input stream stream as if by calling fgetc size times for each object, and storing the results, in the order obtained, into the successive positions of buffer, which is reinterpreted as an array of unsigned char. The file position indicator for the stream is advanced by the number of characters read.
# Declarations
size_t fread( void *buffer, size_t size, size_t count,
FILE *stream );
(until C99)
size_t fread( void *restrict buffer, size_t size, size_t count,
FILE *restrict stream );
(since C99)
# Parameters
buffer: pointer to the array where the read objects are storedsize: size of each object in bytescount: the number of the objects to be readstream: the stream to read
# Return value
Number of objects read successfully, which may be less than count if an error or end-of-file condition occurs.
# Example
#include <stdio.h>
enum { SIZE = 5 };
int main(void)
{
const double a[SIZE] = {1.0, 2.0, 3.0, 4.0, 5.0};
printf("Array has size %ld bytes, element size: %ld\n", sizeof a, sizeof *a);
FILE *fp = fopen("test.bin", "wb"); // must use binary mode
fwrite(a, sizeof *a, SIZE, fp); // writes an array of doubles
fclose(fp);
double b[SIZE];
fp = fopen("test.bin","rb");
const size_t ret_code = fread(b, sizeof b[0], SIZE, fp); // reads an array of doubles
if (ret_code == SIZE)
{
printf("Array at %p read successfully, contents:\n", (void*)&a);
for (int n = 0; n != SIZE; ++n)
printf("%f ", b[n]);
putchar('\n');
}
else // error handling
{
if (feof(fp))
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fp))
perror("Error reading test.bin");
}
fclose(fp);
}