C fputs() and fgets()

Writing File : fputs() function

The fputs() function writes a line of characters into file. It outputs string to a stream.

Syntax:




int fputs(const char *s, FILE *stream)  



Example:




#include<stdio.h>
#include<conio.h>
void main(){  
FILE *fp;  
clrscr();  
  
fp=fopen("myfile2.txt","w");  
fputs("hello c programming",fp);  
  
fclose(fp);  
getch();  
}  



myfile2.txt

hello c programming

Reading File : fgets() function

The fgets() function reads a line of characters from file. It gets string from a stream.

Syntax:




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



Example:


#include<stdio.h>
#include<conio.h>
void main(){  
FILE *fp;  
char text[300];  
clrscr();  
  
fp=fopen("myfile2.txt","r");  
printf("%s",fgets(text,200,fp));  
  
fclose(fp);  
getch();  
}  



Output:

hello c programming




Instagram