fread(3S)


fread, fwrite -- binary input/output

Synopsis

   #include <stdio.h>
   

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

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

Description

fread reads into an array pointed to by ptr up to nitems items of data from stream, where an item of data is a sequence of bytes (not necessarily terminated by a null byte) of length size. fread stops reading bytes if an end-of-file or error condition is encountered while reading stream, or if nitems items have been read. fread increments the data pointer in stream to point to the byte following the last byte read if there is one. fread does not change the contents of stream. fread returns the number of items read.

fwrite writes to the named output stream at most nitems items of data from the array pointed to by ptr, where an item of data is a sequence of bytes (not necessarily terminated by a null byte) of length size. fwrite stops writing when it has written 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. fwrite increments the data-pointer in stream by the number of bytes written. fwrite returns the number of items written.

Return values

If size or nitems is zero, then fread and fwrite return a value of 0 and do not effect the state of stream.

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

Errors

If an error occurs, the error indicator for stream is set.

fread fails if the file is a regular file and an attempt was made to read at or beyond the offset maximum associated with the corresponding stream.

fwrite fails when either the named stream is unbuffered or its buffer needed to be flushed and an attempt was made to write at or beyond the offset maximum.

References

abort(3C), exit(2), fclose(3S), fopen(3S), fprintf(3S), fscanf(3S), getc(3S), gets(3S), intro(3), Intro(3S), lseek(2), putc(3S), puts(3S), read(2), write(2)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004