My Subject Blog

C Program for FILE Function fputc fputs fgets

/*
Write a program to illustrate the use of fputc(), fputs(), and fgets().
*/

#include<stdio.h>
#include<string.h>
int main() 
{
   
 FILE *fp;
 char str[4];
 char ch[4]={'a','b','c','d'};
 int i;

 fp=fopen("Test.txt","w");
 if(fp == NULL) 
 {
  perror("Error opening file");
  return(-1);
 }
 for(i=0;i<4;i++)
 {
fputc(ch[i],fp); //use of fputc()
 }
 fputs("EFGH",fp);   //use of fputs()
 fclose(fp);

   
 fp=fopen("Test.txt","r");

  if(fp == NULL) 
 {
  perror("Error opening file");
  return(-1);
 } 
 while(fgets(str,4,fp)!=NULL)  //use of fgets()
 {
printf("%s",str);
 }
 fclose(fp);
   
return 0;

Comments

Post a Comment

Most Visited Post

C Program to Find Largest Odd Number from Array

Followers