Open pre-existing file and append string to file in C

# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
   FILE *fp;
   char c;
   clrscr();
 
   printf("File before appending :\n");
 
   fp=fopen("mydata.txt","r");
   while(!feof(fp))
   {
	  c=fgetc(fp);
	  printf("%c",c);
   }
   fp=fopen("mydata.txt","a");
 
   if(fp==NULL)
   {
	  printf("File can not appended");
	  exit(1);
   }
   printf("\n Enter string to append :");
 
   while(c!='.')
   {
	  c=getche();
	  fputc(c,fp);
   }
   fclose(fp);
 
   printf("\n File After appending :\n");
 
   fp=fopen("mydata.txt","r");
 
   while(!feof(fp))
   {
	  c=fgetc(fp);
	  printf("%c",c);
   }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.