C program to merge two files to third file

Quick links

Write a C program to merge contents of two files to third file. Logic to merge two files to third file in C program. How to merge contents of two files to third file in C programming.

Required knowledge

Basic Input Output, Pointers, Strings, File Handling

Logic to merge two files to third file

Step by step descriptive logic to merge two files to third file.

  1. Open both source files in r (read) and destination file in w (write) mode.
  2. Copy file contents from both source files one by one to destination file.
  3. Close all files to save and release all resources.

Program to merge two files to third file

/**
 * C program to merge contents of two files to third file.
 */

#include <stdio.h>
#include <stdlib.h>


int main()
{
    FILE *sourceFile1;
    FILE *sourceFile2;
    FILE *destFile;
    char sourcePath1[100];
    char sourcePath2[100];
    char destPath[100];

    char ch;

    /* Input path of files to merge to third file */
    printf("Enter first source file path: ");
    scanf("%s", sourcePath1);
    printf("Enter second source file path: ");
    scanf("%s", sourcePath2);
    printf("Enter destination file path: ");
    scanf("%s", destPath);

    /* 
     * Open source files in 'r' and 
     * destination file in 'w' mode 
     */
    sourceFile1 = fopen(sourcePath1, "r");
    sourceFile2 = fopen(sourcePath2, "r");
    destFile    = fopen(destPath,    "w");


    /* fopen() return NULL if unable to open file in given mode. */
    if (sourceFile1 == NULL || sourceFile2 == NULL || destFile == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open file.\n");
        printf("Please check if file exists and you have read/write privilege.\n");

        exit(EXIT_FAILURE);
    }


    /* Copy contents of first file to destination */
    while ((ch = fgetc(sourceFile1)) != EOF)
        fputc(ch, destFile);

    /* Copy contents of second file to destination */
    while ((ch = fgetc(sourceFile2)) != EOF)
        fputc(ch, destFile);

    printf("\nFiles merged successfully to '%s'.\n", destPath);


    /* Close files to release resources */
    fclose(sourceFile1);
    fclose(sourceFile2);
    fclose(destFile);

    return 0;
}

Contents of data\file1.txt

Hurray!!! I learned to create file in C programming. I also learned to write contents to file. Next, I will learn to read contents from file on Codeforwin. Happy coding ;)

Contents of data\file2.txt

Reading a file line by line.
--------------------------------------------
I love programming in C.
Learning programming on Codeforwin is easy.

Contents of data\merged-file.txt

Hurray!!! I learned to create file in C programming. I also learned to write contents to file. Next, I will learn to read contents from file on Codeforwin. Happy coding ;)
Reading a file line by line.
--------------------------------------------
I love programming in C.
Learning programming on Codeforwin is easy.

Output

Enter first source file path: data\file1.txt
Enter second source file path: data\file2.txt
Enter destination file path: data\merged-file.txt

Files merged successfully to 'data\merged-file.txt'.

Happy coding 😉