Write and Read String from File

#include<stdio.h>
#include<conio.h>
void main()
{
  FILE *fp1;
  char *s,ch;
  int c=0;
  clrscr();
  fp1=fopen("string1.txt","w");
  do
  {
    fflush(stdin);
    printf("\n Enter any string : ");
    gets(s);
    fputs(s,fp1);
    printf("\n Do you want to continue [y-n] : ");
    ch=getchar();
    if(ch=='y')
     {
          fputs("\n",fp1);
     }
  }while(ch=='y');
  fclose(fp1);
  if((fp1=fopen("string1.txt","r"))==NULL)
   {
      printf("\n Error " );
      exit(0);
   }
   while(!(feof(fp1)))
    {
       fgets(s,50,fp1);
       printf("\n %s",s);
       c++;
    }
   printf("\n Total number of lines : %d",c);
   getch();
  }

Post a Comment

Previous Post Next Post