0% found this document useful (0 votes)
2 views

Program 12 (1)

This program merges the contents of two text files, 'file1.txt' and 'file2.txt', into a new file called 'merged.txt'. It checks for errors when opening the files and reports any issues. If successful, it reads the contents of both files and writes them into the merged file, confirming the operation upon completion.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 12 (1)

This program merges the contents of two text files, 'file1.txt' and 'file2.txt', into a new file called 'merged.txt'. It checks for errors when opening the files and reports any issues. If successful, it reads the contents of both files and writes them into the merged file, confirming the operation upon completion.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM 12

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main()
{
clrscr();
char file1[] = "file1.txt";
char file2[] = "file2.txt";
char mergedFile[] = "merged.txt";

ifstream fin1(file1);
ifstream fin2(file2);
ofstream fout(mergedFile);
if (!fin1)
{
cout << "Error opening file " << file1 << endl;
getch();
return;
}
if (!fin2)
{
cout << "Error opening file " << file2 << endl;
getch();
return;
}
if (!fout)
{
cout << "Error opening merged file for writing" << endl;
getch();
return;
}
char ch;
while (fin1.get(ch))
{
fout.put(ch);
}
while (fin2.get(ch))
{
fout.put(ch);
}
fin1.close();
fin2.close();
fout.close();
cout << "Files merged successfully into " << mergedFile << endl;
getch();
}

You might also like