This is a simple C program in File Handling to copy content of one file and append that content to another file.
As you can see below two files we have used 1. file1.txt and 2. file2.txt
First of all let us see content of both these files.
**** Content of file1.txt Before Execution****
**** Content of file2.txt Before Execution ****
After executing the following C program you will find the content of file.txt is copied and appended to file2.txt
C PROGRAM
#include<studio.h>
#include<stdlib.h>
Int main()
{
FILE *f1,*f2;
Char c;
Char fname1[50];
Char fname2[50];
printf("\n Enter a filename to open for reading: ");
scanf("%s",fname1);
printf("\n Enter a filename to open for append content of first file: ");
scanf("%s",fname2);
f1=fopen(fname1,"r");
If (f1==NULL)
{
printf("\n %s file does not exist",fname1);
exit(0);
}
f2=fopen(fname2,"a");
if (f2==NULL)
{
printf("\n %s file does not exist",fname2);
exit(0);
}
c=fgetc(f1);
while(c!=EOF)
{
fputc(c,f2);
c=fgetc(f1);
}
printf("\n Content in %s is appended to %s",fname1,fname2);
fclose(f1);
fclose(f2);
return 0;
}
INPUT && OUTPUT
Now let us see the content of file2.txt after executing the above program.
**** Content of file2.txt After Execution ****
As you can see in above picture you will find content of file1 is copied and appended to file2.txt.
0 Comments