Namespaces
Variants
Views
Actions

fread

From cppreference.com
< c‎ | io
 
 
File input/output
Types and objects
Functions
File access
Direct input/output
fread
Unformatted input/output
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)
Formatted input
(C99)(C99)(C99)(C11)(C11)(C11)     
 
Defined in header <stdio.h>
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)

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.

If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. If a partial element is read, its value is indeterminate.

Contents

[edit] Parameters

buffer - pointer to the array where the read objects are stored
size - size of each object in bytes
count - the number of the objects to be read
stream - the stream to read

[edit] Return value

Number of objects read successfully, which may be less than count if an error or end-of-file condition occurs.

If size or count is zero, fread returns zero and performs no other action.

fread does not distinguish between end-of-file and error, and callers must use feof and ferror to determine which occurred.

[edit] Example

#include <stdio.h>
 
enum { SIZE = 5 };
int main(void)
{
    double a[SIZE] = {1.,2.,3.,4.,5.};
    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");
    size_t ret_code = fread(b, sizeof *b, SIZE, fp); // reads an array of doubles
    if(ret_code == SIZE) {
        puts("Array read successfully, contents: ");
        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);
}

Output:

Array read successfully, contents: 
1.000000 2.000000 3.000000 4.000000 5.000000

[edit] References

  • C17 standard (ISO/IEC 9899:2018):
  • 7.21.8.1 The fread function (p: 243-244)
  • C11 standard (ISO/IEC 9899:2011):
  • 7.21.8.1 The fread function (p: 335)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.19.8.1 The fread function (p: 301)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.9.8.1 The fread function

[edit] See also

reads formatted input from stdin, a file stream or a buffer
(function) [edit]
gets a character string from a file stream
(function) [edit]
writes to a file
(function) [edit]