fread(S) 6 January 1993 fread(S) Name fread, fwrite - binary input/output Syntax cc . . . -lc #include <stdio.h> #include <sys/types.h> size_t fread (ptr, size, nitems, stream) void *ptr; size_t size; int nitems; FILE *stream; size_t fwrite (ptr, size, nitems, stream) void *ptr; size_t size; int nitems; FILE *stream; SVID Syntax int fread (ptr, size, nitems, stream) void *ptr; size_tsize; int nitems; FILE *stream; int fwrite (ptr, size, nitems, stream) void *ptr; size_t size; int nitems; FILE *stream; Description The fread function copies nitems items of data from the named input stream into an array pointed to by ptr. An item of data is a sequence of bytes (not necessarily terminated by a null byte) of length size. fread stops appending bytes if an end-of-file or error condition is encountered while reading stream, or if nitems items have been read. fread leaves the file pointer in stream, if defined, pointing to the byte following the last byte read if there is a byte. fread does not change the con- tents of stream. fwrite appends at most nitems items of data from the array pointed to by ptr to the named output stream. fwrite stops appending when it has appended nitems items of data or if an error condition is encountered on stream. fwrite does not change the contents of the array pointed to by ptr. The argument size is typically sizeof(*ptr) where the pseudo-function sizeof specifies the length of an item pointed to by ptr. If ptr points to a data type other than char, it should be cast into a pointer to char. Diagnostics The fread and fwrite functions return the number of items read or writ- ten. If nitems is non-positive, no characters are read or written, and 0 is returned by both fread and fwrite. See also fopen(S), getc(S), gets(S), printf(S), putc(S), puts(S), read(S), scanf(S), stdio(S) write(S) Standards conformance fread and fwrite are conformant with: AT&T SVID Issue 2; X/Open Portability Guide, Issue 3, 1989; ANSI X3.159-1989 Programming Language -- C; Intel386 Binary Compatibility Specification, Edition 2 (iBCSe2); IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1); and NIST FIPS 151-1.