Merging two files into one file

Some time we need to merge two or more file written by many ones. Then we can simply use such a lite and small program to merge many files into one.

Screenshots:

The output of the program
View of the file one

View the file two

View the merged file here



The code:
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

int main(int argc, char *argv[])
{
 if ( argc != 4 )
 cout<<"give 3 file as command line argument"<<endl;
 
 else {
    
 ifstream file1;   /*object created for file1*/
 file1.open(argv[1]);      /*Name of the file1 passed through command line argument*/
 string line1;      /*String is created to store all the data of the file1*/
 char c1 = file1.eof();  /*EOF file1 is stored in a character variable*/
 getline(file1,line1,c1);    /*scanning and storing the data into the string*/
 cout<<"The first file is opening here:\n\n";
 cout<<line1<<endl;
 /*Similarly other file operation is done*/
 ifstream file2;
 file2.open(argv[2]);
 string line2;
 char c2 = file2.eof();
 getline(file2,line2,c2);
 cout<<"The second file is opening here:\n\n";
 cout<<line2<<endl;
 string line3;
 string line ("\n");
 line3=line1+line+line2;
 ofstream result;
 result.open(argv[3]);   /*new file is created as the given name of the merged file*/
 result<<line3<<endl;

/*files are closing here*/
 file1.close();         
 file2.close();
 result.close();

/*Printing the data of the new merged file*/
 ifstream result1;
 result1.open(argv[3]);
 string line4;
 char c3 = file1.eof();
 getline(result1,line4,c3);
 cout<<"After merging two notepad text files into a another new notepad text file.\nThe new file is opening here:\n\n";
 cout<<line4<<endl;
 }


 cout<<endl<<endl<<endl<<"*************THANK YOU*************"<<endl;
 return 0;
}


/* INSTRUCTION
Give 3 file names as command line argument eg ./a.out text_file1.txt text_file2.txt merge_file_file1_file2.txt
*/

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java