Last modified on 27 February 2014, at 17:16

C Programming/C Reference/stdio.h/fread

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

size_t fread (void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)

The fread function copies nmemb 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 when nmemb items have been read, end of file has been reached, or an error has occurred. Upon returning, fread sets the file pointer in the stream pointing to the byte past the last byte that has been read. The contents of stream remain unchanged. The fread function returns the number of items actually read. If nmemb is zero, no action is taken and the function will return 0.

DiagnosticsEdit

The function may fail with the following error codes:

  • 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.

ReferencesEdit

External linksEdit