fread()

NAME

fread(), fwrite() - binary stream input/output

SYNOPSIS

#include <stdio.h>

int fread (void *ptr, size_t size, size_t nmemb, FILE *stream) int fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)

DESCRIPTION

The function fread(3) reads nmemb objects, each size(1) bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

The function fwrite(3) writes nmemb objects, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr. The functions fread(3) and fwrite(3) advance the file position indicator for the stream by the number of bytes read or written.

RETURN VALUES

Fread(3) and fwrite(3) return the number of objects read or written. If an error occurs, or the end-of-file is reached, the return value is a short object count (or zero).

Because the function fread(3) does not distinguish between end-of-file and error, callers must use feof(3) and ferror(3) to determine which occurred.

The function fwrite(3) returns a value less than nmemb only if a write error has occurred.

ERRORS

Fread(3) can set errno to the following values:

[EBADF]
Stream is not a valid file or socket descriptor open for reading.
[EFAULT]
Ptr points outside the allocated address space.
[EIO]
An I/O error occurred while reading from the file system.
[EINTR]
Before any data arrived, a read from a slow device was interrupted by the delivery of a signal.
[EINVAL]
The pointer associated with stream was negative.
[EAGAIN]
The file was marked for non-blocking I/O, and no data were ready to be read.

Fwrite(3) can set errno to the following values:

[EBADF]
Stream is not a valid descriptor open for writing.
[EPIPE]
An attempt is made to write to a pipe that is not open for reading by any process.
[EFBIG]
An attempt was made to write a file that exceeds the process's file size limit or the maximum file size.
[EFAULT]
Part of the data to be written to the file points outside the process's allocated address space.
[EINVAL]
The pointer associated with stream was negative.
[ENOSPC]
There is no free space remaining on the file system containing the file.
[EIO]
An I/O error occurred while reading from or writing to the file system.
[EAGAIN]
The file was marked for non-blocking I/O, and no data could be written immediately.

SEE ALSO

read(2)

write(2)