Thursday, March 2, 2023

BCA and B.Sc Programs - Programming in C++ Lab - Bharathiar University - Practical Program 12 - C++ Program to merge two files into single file

 

Bharathiar University

Programming in C++ Lab
C++ Program 12
Write a C++ Program to Merge Two Files into Single File



In this Page:

  • C++ Source Code for Visual Studio Code
  • C++ Source Code for MS Dos Turbo C++

FOR VISUAL STUDIO 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 fp1, fp2;
        ofstream fp3;

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

        cout<<"Enter first file name with extension: ";
        cin>>fname1;
        cout<<"\nEnter second file name with extension: ";
        cin>>fname2;
        cout<<"\nEnter target file with extension to copy: ";
        cin>>fname3;

        fp1.open(fname1);
        fp2.open(fname2);
        fp3.open(fname3); 

        if(!fp1 || !fp2 || !fp3)
        {
                perror("\nError Message in file opening ");
                exit(EXIT_FAILURE);
        }

        
        while(!fp1.eof())
        {
                fp1>>noskipws>>ch;
                fp3<<ch;
        }

        while(!fp2.eof())
        {
                fp2>>noskipws>>ch;
                fp3<<ch;
        }

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

        fp1.close();
        fp2.close();
        fp3.close();

        return 0;
}

OUTPUT:

Enter first file name with extension: sample1.txt

Enter second file name with extension: sample2.txt

Enter target file with extension to copy: output.txt

The two files were merged into output.txt file successfully....!!


FOR TURBO C++

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

 

#include<iostream.h>

#include<fstream.h>

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

int main()

{

                ifstream fp1, fp2;

                ofstream fp3;

                clrscr();

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

 

                cout<<"Enter first file name with extension :: ";

                cin>>fname1;

                cout<<"\nEnter second file name with extension :: ";

                cin>>fname2;

                cout<<"\nEnter third file with extension to copy :: ";

                cin>>fname3;

 

                fp1.open(fname1);

                fp2.open(fname2);

                fp3.open(fname3);

 

                if(!fp1 || !fp2 || !fp3)

                {

                                perror("\nError Message in file opening ");

                                exit(EXIT_FAILURE);

                }

 

 

                while(fp1.eof()==0)

                {

                                fp1>>ch;

                                fp3<<ch;

                }

 

                while(fp2.eof()==0)

                {

                                fp2>>ch;

                                fp3<<ch;

                }

 

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

 

                fp1.close();

                fp2.close();

                fp3.close();

                getch();

                return 0;

}

OUTPUT:




No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...