adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C to write and read text in/from file Using fputs()/fgetc() functions

Program in C to write  text in file Using fputs() functions



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 int main()
  {
   FILE *file;
   char str[200];
   file=fopen("Test.txt","w");
   if(file==NULL)
     {
      printf("File is not found.");
      exit(0);
   }
 printf("Enter your contents:-");
 while(strlen(gets(str))>0)
  {
   fputs(str,file);
   fputs("\n",file);
  }
 close(file);
 return 0;
  }
Output:-





Program in C to  read text from file Using fgetc() functions



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 int main()
  {
   FILE *file;
   char ch;
   file=fopen("Test.txt","r");
   if(!file) //if(file==0) 
     {
      printf("File is not found.");
      exit(0);
   }
  printf("Your given contents :-");
  ch=getc(file);
  while(!feof(file)) //while(ch!=EOF)
   {
    putchar(ch);//printf("%c",ch);
    ch=getc(file);
   }
 close(file);
 return 0;
  }
Output:-






Post a Comment

0 Comments