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();

}













Output:


  1. Enter the first file Name:

f1.txt


Enter the second file name:

f2.txt


Contents of f1.txt

The best friend on earth of man is the tree


Contents of f2.txt

To exist as a nation.


Contents of f2.txt after appending:

To exist as a nation.The best friend on earth of man is the tree


  1. Enter the first file Name:

College.txt


Enter the second file name:

School.txt


File College.txt does not exist



Post a Comment

Previous Post Next Post