- C Programming Basics
- C Tutorial
- C Program Structure
- C Basic Syntax
- C Data Types
- C Constants
- C Variables
- C Operators
- C Ternary Operator
- C Storage Classes
- C Flow of Control
- C Decision Making
- C if if-else Statement
- C switch Statement
- C Loops
- C for Loop
- C while Loop
- C do-while Loop
- C goto Statement
- C break Statement
- C continue Statement
- C Popular Topics
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Recursion
- C Scope Rules
- C Programming Advance
- C Structures
- C Unions
- C Bit Fields
- C Enumerations
- C Input & Output
- C Typedef
- C Preprocessors
- C Type Casting
- C Recursion
- C Error Handling
- C Linked Lists
- C Stacks
- C Queues
- C Binary Trees
- C Header Files
- C File I/O
- C Variable Arguments
- C Memory Management
- C Command Line Arguments
- C Programming Examples
- C Programming Examples
- C Programming Test
- C Programming Test
C fgets()
The fgets function reads up to the num-1 characters from stream and stores then in character array pointer to by str. Character are read until either a newline or an EOF is received or until the specified limit is reached. After the characters have been read, a null is stored in the array immediately after the last character read. A newline character will be retained and will be the part of array pointed to by str.
fgets() Syntax
#include<stdio.h> char *fgets(char *str, int num, FILE *stream);
fgets returns str on success, otherwise a null pointer is returned upon failure.
fgets() Example
Following c program uses fgets() function to display the contents of the text file:
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { clrscr(); FILE *fp; char fname[20], str[128]; printf("Enter the file name with extension (like demo.txt) : "); gets(fname); fp=fopen(fname, "r"); if(fp==NULL) { printf("Error in opening the file..!!\n"); printf("Press any key to exit..\n"); getch(); exit(1); } while(!feof(fp)) { if(fgets(str, 126, fp)) { printf("%s",str); } } fclose(fp); getch(); }
« Previous Function Next Function »
Follow/Like Us on Facebook
Subscribe Us on YouTube