fgets() function in C is used to read data from the file. it is similar to gets() in C. Rather than reading a string from input stream like gets() does, fgets() function in C reads a string from file.

What is fgets() function in C?

fgets (file get string function)function is used to get or read the string from file.
fgets() is a file handling function in C which is used to read the content of a file as an array of characters or string.

Like fgetc function, fgets() function in C used to read data from the file but fgetc reads a single character whereas fgets used to read a string from file.

fgets reads it from a specified file, up to and including a newline character. It stores the string in the string variable passed to it, adding a null character to terminate the string.
In c programming for using fgets function, you have to include stdio.h header file.

fgets() function in C
fgets() function in C

fgets syntax in c: –
fgets(char array[],int n,file_pointer);

In above syntax fgets function accepts three parameters which are as follow:

1. First parameter (char array) is character array which used for storing string we are reading from file.

2. Second parameter (int n): – This is an integer variable which stores the size the maximum number of character we want to read from file.

3. Third parameter (file _pointer): – This is the File object which holds the location of file and opening mode in which file opened.

Program to read data from file using fgets() function.

#include<stdio.h>

#include<conio.h >

int main()

{

FILE *fp;

printf("\n opening the File in read mode:");

fp = fopen("technical.txt", "r"); //open file in read mode

char data[50];

if ( fp == NULL )
{
printf( "Could not open file:" ) ;
return 1;
}
printf( "\nReading the file:" ) ;
fgets ( data, 5, fp );
printf( "\nString from file => %s" , data ) ;
printf("\nClosing the file:") ;
fclose(fp) ;

return 0;

}

Program description: –

In the above program, we are reading the string from the file until the end.

1. First, we open the file in reading mode using fopen function which accepts file object and mode.
We write a character array to store string that we are going to read.
We opened file technical.txt which having content (content:- learn Learn C programming: File handling function) in reading mode.

2. Next using fgets function we read the first string form file.
fgets function which accepts 3 parameters. First is an array in which we read the string, second is the maximum number we are going to accepts i. e. 5 and the third parameter is file pointer which keeps track of position in a file.

3. Now print data from the file which is stored in data variable.

Output: –

fgets() function in C

In the above example, we read the single string from the file. Now let us see a program which read whole file data using fgets() function.

Program to read data from file in c

#include<stdio.h>

#include<conio.h >

int main()

{

FILE *fp;

printf("\n opening the File in read mode:");

fp = fopen("technical.txt", "r"); //open file in read mode

char data[50];

printf("\n Reading from file:");

if (fp != NULL) {

// Check whether it is the last character or not

while (!feof(fp)) {

// fgets function to read string from file

fgets(data, 50, fp);

}

printf("\n Data from the File => %s ", data);
printf("\nClosing the file:") ;

fclose(fp);

}

else {

printf("\n Unable to Create or Open the Sample.txt File");

}

return 0;
}

Output: –

fgets() function in C

Write A Comment