Bharathiar University
Programming in C++ LabC++ Program 12Write 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:
/* 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;
}
No comments:
Post a Comment