Home » C programs » stdio.h header file functions

fgets() function of stdio.h in C

In this article, we are going to learn about the fgets() function of stdio.h header file in C programming language and use it while getting the characters from file pointer.
Submitted by Abhishek Sharma, on April 13, 2018

If you are handling files in your application then reading from a file is something that you cannot ignore. Therefore, the function fgets() will help you doing this.

The Function is use to read a string from a file. The Function needs only three parameters, first a character array, second is the number of character you want to read and the third is the file itself.


    fgets (str, 60, F); 
    //Here, str is a character array, F is the file variable.

It will read 60 characters from the file from the current position of the cursor.

stdio.h - fgets() function Example in C

#include <stdio.h>

int main ()
	//initializing the file pointer 
	//and type of variables
	char str[60];

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

	if(F == NULL) {
		perror("Error is:");
	if( fgets (str, 60, F)!=NULL ) {
		//printing the output on console



stdio.h - fgets() in c language

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com (2015-2018), Some rights reserved.

close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.