Write a C Program for Text File Handling
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp; // declaration of file pointer
char c;
clrscr();
fp=fopen("fileinC.txt","w"); // opening a new file in write mode
if(fp==NULL) // checking for file opening error
{ printf("\n\nFile opening error!");
getch();
exit(0);
}
printf("\nEnter the text for file?\n[Press Ctrl+Z to terminate]\n");
while((c=getchar())!= EOF) //read character from keyboard
putc(c,fp); //put the entered character into file
fclose(fp); //close the file
fp=fopen("fileinC.txt","r"); //reopen the file in read mode
while((c=getc(fp))!= EOF) //reading single character from file until EOF found
printf("%c",c);
fclose(fp); //finally closing the file
getch();
}
Comments
Post a Comment