Use w+ mode for writing and reading a file in C

# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
   FILE *fp;
   char c=' ';
   clrscr();
   fp=fopen("mydata.txt","w+");
   if(fp==NULL)
   {
	  printf("Can not open file");
	  exit(1);
   }
   printf("press '.' to exit :");
   while(c!='.')
   {
	  c=getche();
	  fputc(c,fp);
   }
   rewind(fp);
   printf("\n Contents read :");
 
   while(!feof(fp))
   printf("%c",getc(fp));
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.