Program to merge two files and write them to another file.

Here, we have a basic program example to merge two files and copy the content in another file using different languages. This program is created in c language, c++, Java, and Python.

Code to merge two files in C language

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

void main()
{
	FILE *fold1, *fold2, *fnew;
	char ch, fname1[20], fname2[20], fname3[30];
	printf(" Input the 1st file name : ");
	scanf("%s",fname1);
	printf(" Input the 2nd file name : ");
	scanf("%s",fname2);
	printf(" Input the new file name where to merge the above two files : ");
	scanf("%s",fname3);
	fold1=fopen(fname1, "r");
	fold2=fopen(fname2, "r");
	if(fold1==NULL || fold2==NULL)
	{
		printf(" File does not exist or error in opening...!!\n");
		exit(EXIT_FAILURE);
	}
	fnew=fopen(fname3, "w");
	if(fnew==NULL)
	{
		printf(" File does not exist or error in opening...!!\n");
		exit(EXIT_FAILURE);
	}
	while((ch=fgetc(fold1))!=EOF)
	{
		fputc(ch, fnew);
	}
	while((ch=fgetc(fold2))!=EOF)
	{
		fputc(ch, fnew);
	}
	printf(" The two files merged into %s file successfully..!!\n\n", fname3);
	fclose(fold1);
	fclose(fold2);
	fclose(fnew);
}

Code to merge two files in C++ language

#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
    char fileOne[30], fileTwo[30], fileTarget[30], ch;
    fstream fpsOne, fpsTwo, fpTarget;
    cout<<"Enter the Name of First Source File: ";
    cin>>fileOne;
    cout<<"Enter the Name of Second Source File: ";
    cin>>fileTwo;
    fpsOne.open(fileOne, fstream::in);
    fpsTwo.open(fileTwo, fstream::in);
    if((!fpsOne) || (!fpsTwo))
    {
        cout<<"\nError Occurred (First Source File)!";
        return 0;
    }
    else
    {
        cout<<"\nEnter the Name of Target File: ";
        cin>>fileTarget;
        fpTarget.open(fileTarget, fstream::out);
        if(!fpTarget)
            cout<<"\nError Occurred (Target File)!";
        else
        {
            while(fpsOne>>noskipws>>ch)
                fpTarget<<ch;
            fpTarget<<"\n";
            while(fpsTwo>>noskipws>>ch)
                fpTarget<<ch;
            cout<<"\nContent of Two File Merged Successfully into Third!";
        }
    }
    fpsOne.close();
    fpsTwo.close();
    fpTarget.close();
    cout<<endl;
    return 0;
}

Code to merge two files in Python language

print("Enter the Name of First File: ", end="")
fileOne = input()
print("Enter the Name of Second File: ", end="")
fileTwo = input()
print("Enter the Name of Third File: ", end="")
fileThree = input()

content = ""
fh = open(fileOne, "r")
for line in fh:
    content = content + line + '\n'
fh.close()

fh = open(fileTwo, "r")
for line in fh:
    content = content + line + '\n'
fh.close()

fh = open(fileThree, "w")
fh.write(content)

print("\nFile merged successfully!")

Code to merge two files in Java language

import java.io.*;
import java.util.Scanner;

public class FileMerge
{
   public static void main(String[] args)
   {
      String fileOne, fileTwo, fileThree, line, content="";
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Name of First File: ");
      fileOne = scan.nextLine();
      System.out.print("Enter the Name of Second File: ");
      fileTwo = scan.nextLine();
      System.out.print("Enter the Name of Third File: ");
      fileThree = scan.nextLine();
      try
      {
         FileReader frOne = new FileReader(fileOne);
         BufferedReader brOne = new BufferedReader(frOne);
         FileReader frTwo = new FileReader(fileTwo);
         BufferedReader brTwo = new BufferedReader(frTwo);
         
         for(line=brOne.readLine(); line!=null; line=brOne.readLine())
            content = content + line + "\n";
         brOne.close();
         
         for(line=brTwo.readLine(); line!=null; line=brTwo.readLine())
            content = content + line + "\n";
         brTwo.close();
         
         try
         {
            FileWriter fw = new FileWriter(fileThree, true);
            fw.write(content);
            fw.close();
            System.out.println("\nSuccessfully merged the content of two files into the third file");
         }
         catch(IOException ioe)
         {
            System.out.println("\nSomething went wrong!");
            System.out.println("Exception: " +ioe);
         }
      }
      catch(IOException ioe)
      {
         System.out.println("\nSomething went wrong!");
         System.out.print("Exception: " +ioe);
      }
   }
}