C++ Program to Merge Two Files into a Single file

By | 05.01.2017

Merge Two Files into a Single file


Write a C++ Program to Merge Two Files into a Single file using File Handling. Here’s simple Program to Merge Two Files into a Single file using File Handling in C++ Programming Language.


Below is the source code for C++ Program to Merge Two Files into a Single file using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to Merge Two Files into a Single file  */

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

int main()
{
        ifstream ifiles1, ifiles2;
        ofstream ifilet;

        char ch, fname1[100], fname2[100], fname3[100];

        cout<<"Enter first file name :: ";
        cin>>fname1;
        cout<<"\nEnter second file name :: ";
        cin>>fname2;
        cout<<"\nEnter third name of file :: ";
        cin>>fname3;

        ifiles1.open(fname1);
        ifiles2.open(fname2);

        if(!ifiles1 || !ifiles2)
        {
                perror("\nError Message in file1111 ");
                cout<<"\nPress any key to exit...\n";

                exit(EXIT_FAILURE);
        }

        ifilet.open(fname3);

        if(!ifilet)
        {
                perror("\nError Message ");
                cout<<"\nPress any key to exit...\n";

                exit(EXIT_FAILURE);
        }

        while(ifiles1.eof()==0)
        {
                ifiles1>>ch;
                ifilet<<ch;
        }

        while(ifiles2.eof()==0)
        {
                ifiles2>>ch;
                ifilet<<ch;
        }

        cout<<"\nThe two files were merged into "<<fname3<<" file successfully....!!\n";

        ifiles1.close();
        ifiles2.close();
        ifilet.close();

        return 0;
}

OUTPUT : :


/*  C++ Program to Merge Two Files into a Single file  */

Enter first file name :: C:\\Users\\acer\\Documents\\file4.txt

Enter second file name :: C:\\Users\\acer\\Documents\\file5.txt

Enter third name of file :: C:\\Users\\acer\\Documents\\file6.txt

The two files were merged into C:\\Users\\acer\\Documents\\file6.txt file successfully....!!

Process returned 0

Above is the source code for C++ Program to Merge Two Files into a Single file which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.3 4 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
SARTHAK GUPTA

by the above code solution ,the spaces will not come in the merged file !….any other solution?