C Programming
Computer Programming

C file handling program to append the content of one file to the end of another file print the modified content file



Write C file handling program to append the content of one file to the end of another file print the modified content file

\* C file handling program to append the content of one file to the end of another file print the modified content file *\

# include < stdio.h >
# include < string.h >
int   main( )
{

FILE* fp1 ;
FILE* fp2 ;
char ch, filename1[30], filename2[30] ;

printf("\n Enter the First filename : ") ;
scanf("%s", filename1) ;
fp1 = fopen(filename1, "r+") ;
if (fp1 == NULL)
{
      printf("\nUnable to open file\n") ;
      return -1 ;
}

printf("\n Enter the Second filename : ") ;
scanf("%s", filename2 ) ;
fp2 = fopen(filename2, "r" ) ;
if (fp2 == NULL)
{
      printf("\nUnable to open file\n") ;
      return -1 ;
}

printf("\n\nContent of file 1: \n") ;
ch = getc(fp1) ;
while (ch != EOF)
{
      printf("%c", ch) ;
      ch = getc(fp1) ;
}

printf("\n\nContent of file 2: \n") ;
ch = getc(fp2) ;
while (ch != EOF)
{
      printf("%c", ch) ;
      ch = getc(fp2) ;
}

fseek(fp2, 0, SEEK_SET) ;
ch = getc(fp2) ;
while (ch != EOF)
{
      putc(ch, fp1) ;
      ch = getc(fp2) ;
}
fclose(fp2) ;
fseek(fp1, 0, SEEK_SET) ;
printf("\n\nModified content of file 1:\n") ;
ch = getc(fp1) ;
while (ch != EOF)
{
      printf("%c", ch) ;
      ch = getc(fp1) ;
}
fclose(fp1) ;
printf("\n") ;
return 0 ;

}

Output of Program :

Output of C file handling program to append the content of one file to the end of another file print the modified content file