Thursday, September 13, 2012

Copying From One File To Another In C

Compiler Used: TurboC
Language: C

This is a simple program to demonstrate file handling in C by copying contents of a.txt to b.txt and displaying on the screen.


#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
char c;
FILE *fp1, *fp2;

fp1 = fopen("a.txt", "r");
fp2 = fopen("b.txt", "w");
while((c = getc(fp1))!=EOF){
        putc(c, fp2);
}
fclose(fp1);
fclose(fp2);
fp2 = fopen("b.txt", "r");
while((c=getc(fp2))!=EOF){
        putchar(c);
}
fclose(fp2);
getch();
}

PS: The program can be made better in a lot of ways. Suggestions/Comments are welcome.

No comments:

Post a Comment