Fread

From Wikipedia, the free encyclopedia

The correct title of this article is fread. The initial letter is shown capitalized due to technical restrictions.

fread is a function that reads buffered binary input from a file. It is included in the stdio.h header file in the standard C library.

int fread ((void*) ptr, int size, int nitems, FILE* stream)

The fread function copies nitems items of data of size size 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 contents of stream.

[edit] Diagnostics

The fread function returns the number of items actually read. If nitems is non-positive, no characters are read or written, and 0 is returned by both fread and fwrite.

fread( ) will fail if data needs to be read and:

[EAGAIN] Cannot read the input stream immediately without blocking the process, and the O_NONBLOCK flag is set for the file descriptor associated with stream.

[EBADF] Not a valid file descriptor open for reading.

[EINTR] The read operation was terminated by a signal before any data was read.

[EIO] Cannot read from the controlling terminal. This happens when the process is in a background process group and the attempt by the process to read from its controlling terminal fails, either because the process group is orphaned, or because the process is ignoring or blocking the SIGTTIN signal.

[ENOMEM] Insufficient storage space is available.

[ENXIO] Attempt to read from a non-existent device, or from a device whose capabilities are exceeded.

[edit] References