Write a program to add the contents of one file at the end of another.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *firstFilePtr;
FILE *secondFilePtr;
char firstFileName[20];
char secondFileName[20];
char letter;
printf("Enter the file name of the file for reading: ");
gets(firstFileName);
// Open one file for reading
firstFilePtr = fopen(firstFileName, "r");
if (firstFilePtr == NULL)
{
printf("%s file does not exist...", firstFileName);
exit(0);
}
printf("Enter the file name to append the content : ");
gets(secondFileName);
// Open another file for appending content
secondFilePtr = fopen(secondFileName, "a");
if (secondFilePtr == NULL)
{
printf("%s file does not exist...", secondFileName);
exit(0);
}
// Read the content from the file
letter = fgetc(firstFilePtr);
fputc('\n',secondFilePtr);
while (letter != EOF)
{
fputc(letter,secondFilePtr);
letter = fgetc(firstFilePtr);
}
fclose(firstFilePtr);
fclose(secondFilePtr);
printf("\nThe contents of the file \"%s\" has been added at the end of \"%s\".\n",firstFileName,secondFileName);
getchar();
getchar();
return 0;
}
Comments
Leave a comment