C Program to append the contents of a text file to another existing file. Also check for the existence of the inputed file Names. Display the contents from each.
#include<stdio.h>
main()
{
FILE *f1,*f2;
char c;
char fname1[20],fname2[20];
clrscr();
printf("\nEnter the first file Name:\n");
scanf("%s",fname1);
printf("\nEnter the second file name:\n");
scanf("%s",fname2);
f1=fopen(fname1,"r");
if(f1==NULL)
{
printf("\nFile %s does not exist\n",fname1);
exit();
}
f2=fopen(fname2,"r");
if(f2==NULL)
{
printf("\nFile %s does not exist",fname2);
exit();
}
printf("\n\nContents of %s\n",fname1);
f1=fopen(fname1,"r");
while((c=getc(f1))!=EOF)
{
putchar(c);
}
fclose(f1);
printf("\n\nContents of %s\n",fname2);
f2=fopen(fname2,"r");
while((c=getc(f2))!=EOF)
{
putchar(c);
}
fclose(f2);
f1=fopen(fname1,"a");
f2=fopen(fname2,"r");
while((c=getc(f1))!=EOF)
{
putc(c,f2);
}
fclose(f1);
fclose(f2);
printf("\n\nContents of %s after appending:\n",fname2);
f2=fopen(fname2,"r");
while((c=getc(f2))!=EOF)
{
putchar(c);
}
fclose(f2);
getch();
}
Comments
Post a Comment