FAT | RFS | XFS | ZFS

read()

PROTOTYPE

#include <posix.h>

ssize_t read(int fid, void *buf, size_t bcnt);

DESCRIPTION

read() reads bytes from the current position of the open regular file identified by fid, advances that position, and writes them to the buffer specified by buf until either bcnt bytes are copied, end-of-file is reached, or an error occurs.

By default, read() updates the file’s data access timestamp. The resulting writes can hurt the performance of streaming reads. To suppress data access timestamp updates, use the FSF_NOATIME flag when a volume is added.

If bcnt is 0 or the current position is at or past end-of-file, read() returns 0. If nothing is read due to an error, it returns -1. Otherwise, it returns the number of bytes read. If an error occurs, errno and the file’s error indicator are set. If end-of-file occurs, the file’s end-of-file indicator is set.

ERROR CODES

EBADF fid is not the descriptor of a file open in read mode.
EFAULT buf equals NULL.
EISDIR fid refers to a directory.
ENXIO The associated volume has been removed.

EXAMPLE

char string[12];
int fid;

/*-----------------------------------------------------------------*/
/* Open the file in read-only mode.                                */
/*-----------------------------------------------------------------*/
fid = open("/flash/temp.txt", O_RDONLY);
  return -1;

/*-----------------------------------------------------------------*/
/* Read 12 characters from the file into temporary buffer.         */
/*-----------------------------------------------------------------*/
if (read(fid, string, 12) != 12)
  return -1;