The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

fread - binary input

 SYNOPSIS



#include <stdio.h>

size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);

 DESCRIPTION

The fread() function reads, into the array pointed to by ptr, up to nitems members whose size is specified by size in bytes, from the stream pointed to by stream. The file position indicator for the stream (if defined) is advanced by the number of bytes successfully read. If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. If a partial member is read, its value is indeterminate.

The fread() function may mark the st_atime field of the file associated with stream for update. The st_atime field will be marked for update by the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(), fread(), fscanf(), getc(), getchar(), gets() or scanf() using stream that returns data not supplied by a prior call to ungetc() or ungetwc().

 RETURN VALUE

Upon successful completion, fread() returns the number of members successfully read which is less than nitems only if a read error or end-of-file is encountered. If size or nitems is 0, fread() returns 0 and the contents of the array and the state of the stream remain unchanged. Otherwise, if a read error occurs, the error indicator for the stream is set and errno is set to indicate the error.

 ERRORS

Refer to fgetc().

 EXAMPLES

None.

 APPLICATION USAGE

The ferror() or feof() functions must be used to distinguish between an error condition and an end-of-file condition.

Because of possible differences in member length and byte ordering, files written using fwrite() are application-dependent, and possibly cannot be read using fread() by a different application or by the same application on a different processor.

 FUTURE DIRECTIONS

None.

 SEE ALSO

feof(), ferror(), fopen(), getc(), gets(), scanf(), <stdio.h>.

DERIVATION

Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]