Open In App

C Program to Merge Contents of Two Files into a Third File

Last Updated : 08 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Merging two files into a third file means copying the data of the two files and pasting them into the third file one after another. In this article, we will learn how to merge the contents of two files using a C program.

The simplest method to merge two files into a third file is by first reading the data from one file character by character using fgetc() and printing it to the third file using fputc(). Then reading the second file using the same method and print to the end of the third file.

Let’s take a look at an example:

C
#include <stdio.h>

int main() {
    // Open the source files in read mode and the destination file in write mode
    FILE *fptr1 = fopen("file1.txt", "r");
    FILE *fptr2 = fopen("file2.txt", "r");
    FILE *fptr3 = fopen("merged.txt", "w");

    // Check if the files were opened successfully
    if (fptr1 == NULL || fptr2 == NULL || fptr3 == NULL) {
        printf("Error opening files!\n");
        return 1;
    }

    // Copy contents of the first file to the third file
    char ch;
    while ((ch = fgetc(fptr1)) != EOF) {
        fputc(ch, fptr3);
    }

    // Copy contents of the second file to the third file
    while ((ch = fgetc(fptr2)) != EOF) {
        fputc(ch, fptr3);
    }

    fclose(fptr1);
    fclose(fptr2);
    fclose(fptr3);
    printf("Contents merged successfully");
    return 0;
}


Assume that following is the content of the files:

file1.txt

This is the first file.
and its data

file2.txt

This is the second file and 
its data

Then the merged.txt will be generated as:

This is the first file.
and its dataThis is the second file and 
its data

Output

Contents Merged Successfully

Explanation: The program opens file1.txt and file2.txt in read mode (“r”) and merged.txt in write mode (“w”). It reads one character at a time from file1.txt using fgetc() and writes it to merged.txt using fputc(). After reaching the end of file1.txt, the same process is repeated for file2.txt.

The above method works well for text files, but it may break in some cases for binary files due to character conversions by fgetc() to fputc(). But C also provide methods to properly read and write binary data.

For Binary Files

If first and second files are binary files, then fread() and fwrite() are more efficient as compared to fgetc() and fputc(). The whole process will remain same as the above.

C
#include <stdio.h>

int main() {
    FILE *fptr1 = fopen("file1.bin", "rb");
    FILE *fptr2 = fopen("file2.bin", "rb");
    FILE *fptr3 = fopen("merged.bin", "wb");

    if (fptr1 == NULL || fptr2 == NULL || fptr3 == NULL) {
        printf("Error opening files!\n");
        return 1;
    }

    // Copy contents of the first file to the third file
    char buffer[1024];
    size_t n;
    while ((n = fread(buffer, 1, sizeof(buffer), fptr1)) > 0) {
        fwrite(buffer, 1, n, fptr3);
    }

    // Copy contents of the second file to the third file
    while ((n = fread(buffer, 1, sizeof(buffer), fptr2)) > 0) {
        fwrite(buffer, 1, n, fptr3);
    }

    fclose(fptr1);
    fclose(fptr2);
    fclose(fptr3);

    printf("Contents merged successfully");
    return 0;
}


Output

Contents Merged Successfully


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg
  翻译: