Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The fgets() function

Syntax

char *fgets(char *s, int n, FILE *fp)

This function is used to read a line of characters from the file.
Second argument int n is used to decide the number of characters that should be read from the input stream referred by fp.
It reads n-1 character from the input stream.

Example

Inside file4.txt

C Programming Language fgets Function in File Handling Example

#include<stdio.h>
int main()
{
    FILE *fp;
    char str[50];
    fp=fopen("D:\\C Programming\\file4.txt","r");
    if(fp==NULL)
    {
        printf("\n Unable to open the FILE");
        return 1;
    }
	
    while(fgets(str,50,fp)!=NULL)
    {
        printf("%s",ch);
    }
    fclose(fp);
    return 0;
}

Output

C Programming Language fgets Function in File Handling Example Output
 

Leave a reply