fgets
Header: <stdio.h>
Reads at most count - 1 characters from the given file stream and stores them in the character array pointed to by str. Parsing stops if a newline character is found (in which case str will contain that newline character) or if end-of-file occurs. If bytes are read and no errors occur, writes a null character at the position immediately after the last character written to str.
# Declarations
char* fgets( char* str, int count, FILE* stream );
(until C99)
char* fgets( char* restrict str, int count, FILE* restrict stream );
(since C99)
# Parameters
str: pointer to an element of a char arraycount: maximum number of characters to write (typically the length of str)stream: file stream to read the data from
# Return value
str on success, null pointer on failure.
# Notes
POSIX additionally requires that fgets sets errno if a read error occurs.
Although the standard specification is unclear in the cases where count <= 1, common implementations do
# Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE* tmpf = tmpfile();
fputs("Alan Turing\n", tmpf);
fputs("John von Neumann\n", tmpf);
fputs("Alonzo Church\n", tmpf);
rewind(tmpf);
char buf[8];
while (fgets(buf, sizeof buf, tmpf) != NULL)
printf("\"%s\"\n", buf);
if (feof(tmpf))
puts("End of file reached");
}