C Programming Language


Declaration: char * fgets(char *str,int num, FILE *stream);

                     The fgets() function reads up to num-1character from stream and store them into a character array pointed to by str. Characters are read until either a newline or an EOF is received or until the specified limit is reached. After the character has been read, a null is stored in the array immediately after the last character is read. A newline character will be retained and will be a part of the array pointed to by str.

                          If successful, fgets() returns str; a null pointer is returned upon failure. If a read error occurs, the content of the array pointed to by str are indeterminate.

For example, the following program uses fgets() to display the content of a file.

#include <stdio.h>
int main()
        FILE *fptr;
        char text[100];
        if((fptr = fopen(“TEST”,”r”))==NULL)
                    printf(“Cannot open file\n”);
                      pritf(“File close error\n”);
         return 0;

fputs() Home fscanf()

c program stats