fputs() and fgets()
In C programming language fputs() and fgets() are used to write and read string from the stream. Let see the example of writing and reading file using fgets() and fgets() function.
Writing File : fputs() function
The fputs() function writes a line of character into the file.
The syntax of fputs() function :
int fputs(const char *s, FILE *stream)
let’s see the example of fputs()
#include<stdio.h> #include<conio.h> void main(){ FILE *fp; clrscr(); fp=fopen("myfile.txt","w"); fputs("hello c programming",fp); fclose(fp); getch(); }
myfile.txt
hello c programming
Reading File : fgets() function
The fgets() function reads a line of characters from the file.
The Syntax of the fgets()
char* fgets(char *s, int n, FILE *stream)
Let’s see the example of fgets()
#include<stdio.h> #include<conio.h> void main(){ FILE *fp; char text[300]; clrscr(); fp=fopen("myfile.txt","r"); printf("%s",fgets(text,200,fp)); fclose(fp); getch(); }
Output
hello c programming
Submit your review | |
The Technical Funda
Average rating: 0 reviews