BlogsDope image BlogsDope

Program to merge contents of two files in third file in C++

Feb. 27, 2021 C C++ FILE 2584

Today we are going to talk about something which you may face when dealing with files. There are a lot of times when you have the need to merge two files in a project. So, today we are here to discuss about a programming solution of this problem.

We are assuming you are familiar with file handling operations like opening a file, closing a file. And different modes of doing it, but if you are not or you want to go though it once you can visit here (File Handling).
So, in simple terms we have two file's already created and we want to merge their content in a third file. Now let's dive into the solution.

Approach:

  1. We have to open the files so that we can fetch the content from there. (There are different modes we can open a file for eg. reading mode, writing mode, appending mode, truncate mode) 
  2. So, we have to open file1 and file 2 in read mode and the third file in writing mode, so that we can write the content we get from file 1 and file 2. 
  3. Fetch the content from file1 first and store it in a string and then push the value in file3.
  4. Repeat step 3 but with file2 now. 
  5. Close all the files.

For fetching data from file we are going to use the getline() function. The C++ getline() is a standard library function that is used to read a string or a line from an input stream. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered.

Code :

Note : We have already created 3 file's naming file1.txt, file2.txt with some content in these two of them and an empty file with name file3.txt

Content in file1.txt :

Content in file2.txt :

And the third file is empty.
Below is the implementation of the approach discussed.

//C++ code
#include<bits/stdc++.h>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
  fstream f1,f2,f3; 
  string str1,str2; //str1 for fetching string line from file 1 and str2 for fetching string from file2
  
  f1.open("file1.txt",ios::in);//opening file in reading mode
  f2.open("file2.txt",ios::in);
  f3.open("file3.txt",ios::out);//opening file in writing mode

   while(getline(f1, str1)){ //read lines from file object and put it into string and push back to file 3.
         f3<<str1; // inserting the fetched string inside file3
         f3<<endl;
      }

   while(getline(f2, str2)){ //Same step for file 2 to file 3 fetching
         f3<<str2;
         f3<<endl;
      }

  f1.close();
  f2.close();
  f3.close();
}
merged two files in one

Note: To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file. Also C++ automatically close and release all the allocated memory. But a programmer should always close all the opened files.


Liked the post?
Hi, I am Priyansh Jha. I am a pre-final year student of Electronics and Communication major undergraduate.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).