fgets()
PROTOTYPE
#include <stdio.h>
char *fgets(char *str, int n, FILE *file);
DESCRIPTION
fgets() reads bytes from the current position of the open regular file identified by file, advances its current position, and writes to the buffer specified by str until either n - 1 bytes are copied, end-of-file is reached (which sets the end-of-file indicator), an error occurs, or a newline is reached (which is copied to the output buffer).
Unless disabled via FSF_NOATIME or only a byte pushed back by ungetc() is read, the file’s data access timestamp is updated.
If end-of-file occurs before any bytes are copied, fgets() returns NULL without modifying the output buffer. Otherwise, the output is terminated by a NULL character. If an error occurs, fgets() sets errno and the file’s error indicator, and then returns NULL. Otherwise, fgets() returns str.
ERROR CODES
| EBADF | file is not the handle of a file open in read mode. |
| EFAULT | str equals NULL. |
| EISDIR | file refers to a directory. |
EXAMPLE
char s[20];
FILE *file;
...
/*-----------------------------------------------------------------*/
/* Read the first 20 chars from stream. */
/*-----------------------------------------------------------------*/
if (fgets(s, 21, file) == NULL)
{
perror("fgets() failed");
return -1;
}