fread()
PROTOTYPE
#include <stdio.h>
size_t fread(void *buf, size_t size, size_t nitems, FILE *file);
DESCRIPTION
fread() reads the open regular file identified by file at its current position and writes to the buffer specified by buf until either nitems elements are copied, end-of-file is reached, or an error occurs. size is the number of bytes in a single element. The file position is advanced by the number of bytes read.
By default, successful fread() calls update the file’s data access timestamp. This causes streaming reads to generate a stream of writes, which can hurt performance. To suppress data access timestamp updates, use the FSF_NOATIME flag when a volume is added.
If size or nitems are 0 or the current position is at or past end of file, fread() returns 0. Otherwise, it returns the number of elements read, which may be 0 if end of file, which sets the end-of-file indicator, or an error, which sets errno and the error flag, occurs. Because fread() can return 0 for errors and end-of-file, use feof() or clearerr()/ferror() to distinguish between these two conditions.
ERROR CODES
| EBADF | file is not the handle of a file open in read mode. |
| EFAULT | buf equals NULL. |
| EISDIR | file refers to a directory. |
EXAMPLE
/*-----------------------------------------------------------------*/
/* Open file for reading. */
/*-----------------------------------------------------------------*/
file = fopen("/flash/temp.txt", "r");
if (file == NULL)
return -1;
/*-----------------------------------------------------------------*/
/* Read the first 20 characters from stream. */
/*-----------------------------------------------------------------*/
if (fread(buffer, 1, 20, file) != 20)
return -1;