Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fread - Read blockwise from a file

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

size_t fread(void *p, size_t elsize, size_t n, FILE *fp);

fread reads n elements, each requiring elsize bytes, from the file with file pointer fp and
stores the data elements read in the area to whose beginning p points. Following a
successful read, the read/write pointer is located after the last byte read.

Return val.

Number of elements actually read, if successful.

This number may be less than n if an error occurs or end of file is reached.

Notes

You must see to it that the area to which p points is sufficient for storing the data elements
read.

To ensure that elsize specifies the correct number of bytes for a data element, you should
use the sizeof function for the size of the data unit to which p points.

fread does not distinguish between end of file and error. Therefore, the feof and ferror
functions should be used before or after each fread call to check whether a correct read
access is possible.

fread reads beyond the newline (\n) character and is therefore specially suitable for
reading in binary files.

Record I/O

fread reads a record (or block) from the current file position.

Number of characters to be read in: n is taken to be the total number of characters to be
read in, i.e.

n = element length * number of elements

If n is greater than the current record length, then only this record is read nevertheless.

If n is less than the current record length, only the first n characters of the record are
read. On the next read access the data of the next record is read.

fread supplies the same return value as for stream I/O, namely the number of elements
read in their entirety. For record I/O it is best to use only element length 1 since in this case
the return value corresponds to the length of the record read (without any record length
field).

The following applies in the case of text files with SAM access mode and variable record
length for which a maximum record length is also specified: When the specification
split=no was entered for fopen, records of maximum record length are not concatenated
with the subsequent record when they are read. By default or with the specification
split=yes, when a record with maximum record length is read, it is assumed that the
following record is the continuation of this record and the records are concatenated.

Example

The following program transfers two personal data items to a file (fwrite) and then reads
in this data again (fread).

#include <stdio.h>
int main(void)
{
  FILE *fp;
  size_t result;
  static struct p
  {
    char name[20];
    int a;
  } person[2] =
    {
      >="ANNE", 30 ̧,
      Ã"JOHN", 60Õ,
    };
  fp = fopen("link=link", "w+r");
  result = fwrite(person, sizeof(struct p), 2, fp);
  printf("%d Personal data written\n", result);
  rewind(fp);
  result = fread(person, sizeof(struct p), 2, fp);
  printf("%d Personal data read\n", result);
  printf("Name1: %s, Age1: %d\n", person[0].name, person[0].a);
  printf("Name2: %s, Age2: %d\n", person[1].name, person[1].a);
  return 0;
}

See also

fwrite, feof, ferror, read, fopen, fopen64, fgetc, fgets, fscanf