program to write data to text file and read 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 mydata.txt");
	  exit(1);
   }
   printf("Write data, press '-' to exit:");
   while(c!='-')
   {
	  c=getche();
	  fputc(c,fp);
   }
   fclose(fp);
   printf("\n Contents read :");
   fp=fopen("mydata.txt","r");
   while(!feof(fp))
   printf("%c",getc(fp));
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.