13/03/2016

C++ Program to Merge Two Files Into Third File Using File Handling

Problem:- C++ Program to Merge Two Files or C Program to merge contents of two files into a third file or Write a C++ Program to Merge Two Files into a Single file or C++ program append the two text files or Merging 2 text files into a 3rd text file in C++ or merging two files into one in c++ and display it contents or Merging Two Files Into One or C program to merge two files or C Program to Merge Two Files Concatenate Lines From Two Text Files Into Output File C program to merge alternate lines from two files or C Program to Append the Content of File at the end of Another.

Check This- Hacker rank Solution for C++ Sub Domain Introduction, Classes, STL, Inheritance.

Explanation:- So in this problem, we have to Merge the two file into an another new file. The program first will ask you to enter the first file name after that ask for entering the second file name than program merge the first file and the second file. We have to enter the both file name with extension cause file always have extension or folder not this is useful when you want to merge two files or in other words, we can say that merge two file. This is not copying one file into the another file in this we have to merge two separate file file1 and file2 into file3 or we can say that we are copying the file1 and file2 in the third file file3.

Also check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.


Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.



Tip:- Before copy the solution I recommended please read this full article, this will help you to build your own logic. If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

Solution:-

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
 //Ghanendra Yadav
 ifstream ifiles1, ifiles2;
 ofstream ifilet;
 char ch, fname1[20], fname2[20], fname3[30];
 cout<<"Enter first file name (with extension like file1.txt) : ";
 gets(fname1);
 cout<<"Enter second file name (with extension like file2.txt) : ";
 gets(fname2);
 cout<<"Enter Third File name of file : ";
 gets(fname3);
 ifiles1.open(fname1);
 ifiles2.open(fname2);
 if(ifiles1==NULL || ifiles2==NULL)
 {
  perror("Error Message ");
  cout<<"Press any key to exit...\n";
  getch();
  exit(EXIT_FAILURE);
 }
 ifilet.open(fname3);
 if(!ifilet)
 {
  perror("Error Message ");
  cout<<"Press any key to exit...\n";
  getch();
  exit(EXIT_FAILURE);
 }
 while(ifiles1.eof()==0)
 {
  ifiles1>>ch;
  ifilet<<ch;
 }
 while(ifiles2.eof()==0)
 {
  ifiles2>>ch;
  ifilet<<ch;
 }
 cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
 ifiles1.close();
 ifiles2.close();
 ifilet.close();
 getch();
}

Output:-

C++ Program to Merge Two Files Into Third File Using File Handling


Input:- FILE 1

C++ Program to Merge Two Files Into Third File Using File Handling


Input:- FILE 2

C++ Program to Merge Two Files Into Third File Using File Handling


FINAL OUTPUT:- AFTER MERGING BOTH ABOVE FILE

C++ Program to Merge Two Files Into Third File Using File Handling


You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

3. Java Program For Find The Gross Salary Of An Employee

4. Hacker Rank Solution For Conditional Statements

5. Hacker Rank Solution For Mini-Max Sum

6. Hacker Rank Solution For Birthday Cake Candles

7. C++ Program For Calculate Percentage Of 5 Subjects

8. Hacker Rank Solution For Strings

9. Hacker Rank Solution For Conditional Statements

10. Hacker Rank solution for 30 Days Of Code



Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: