In this article, we will write a C program to merge the contents of two files into a third file. The program takes the name of the two files from the user, checks if both files exist or not, reads their contents and writes them into a third file.
If the files corresponding to the file names provided by the user do not exist or there is some error opening the file, the program breaks and prints an error on the output window.
Sample Input:
Enter the name of the first file: first_file.txt Enter the name of the second file: second_file.txt Enter the name of the third file: third_file.txt
Sample Output:
The two files merged successfully into file third_file.txt
To merge the contents of the two files into a third file, you have to first open the first file in read mode, read its content character by character, and write it into the third file.
The same steps you have to do for the second file.
Below are the steps you have to follow:
- Open the first file in read mode(“r”) using the
fopen()
function, read its content character by character using thefgetc()
function until the end of the file(EOF) is reached. - Open the second file in read mode(“r”) using the
fopen()
function and also read its content until EOF is reached using thefgetc()
function. - Write the content of the first file into the third file character by character using the
fputc()
function. Follow the same process with the second file. - Once all the above tasks are performed, close all three files using the
fclose()
function.
See implementation in the following C program:
// C program to merge the contents // of two files into a third file #include <stdio.h> int main(){ char file1[100], file2[100], file3[100], ch; FILE *fptr1, *fptr2, *fptr3; printf("Enter the name of the first file: "); scanf("%s", &file1); printf("Enter the name of the second file: "); scanf("%s", &file2); printf("Enter the name of the third file: "); scanf("%s", &file3); // Open the files fptr1 = fopen(file1, "r"); // Read mode fptr2 = fopen(file2, "r"); // Read mode fptr3 = fopen(file3, "w"); // Write mode // Check if there is an error opening the file if(fptr1 == NULL || fptr2 == NULL || fptr3 == NULL){ printf("Error opening the file"); return 1; } // Read the first character of the first file ch = fgetc(fptr1); // Read file1 until EOF(End of file) is reached while(ch!=EOF){ fputc(ch, fptr3); // write into file3 ch = fgetc(fptr1); // Read next character } // Read first character of file 2 ch = fgetc(fptr2); // Read file2 until EOF(End of file) is reached while(ch!=EOF){ fputc(ch, fptr3); // write into file3 ch = fgetc(fptr2); // Read next character } printf("The two files merged successfully into file %s", file3); // Close the files fclose(fptr1); fclose(fptr2); fclose(fptr3); return 0; }
Output:
Enter the name of the first file: first_file.txt Enter the name of the second file: second_file.txt Enter the name of the third file: third_file.txt The two files merged successfully into file third_file.txt Enter the name of the first file: first_file.txt Enter the name of the second file: rando.txt Enter the name of the third file: temp.txxt Error opening the file
Below is a sample output image:
Note: Please note that it is important to close all the files once all operations are performed on the files. This is because open files consume system resources such as file handles and memory. Failing to close them can lead to resource exhaustion, especially in long-running programs or when dealing with a large number of files.
That’s all for this article. Thanks for reading.