We can open more than one files and can write the contents to them. In the following program we have created two different files namely: emp.dat and dept.dat.
Handling
Multiple Files
• We can open more than one files and can write
the contents to them. In the following program we have created two different
files namely: emp.dat and dept.dat. Through our C++ program we
are writing some contents to them. Later on reading those contents and
displaying them on the console. The program is,
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
void main()
{
ofstream out_obj;
//creating
the file for writing purpose
out_obj.open("emp.dat");
//writing
the data to the file
out_obj<<"Rahul\n";
out_obj<<"Lekhana\n";
out_obj<<"Nandan\n";
out_obj<<"Archana\n";
out_obj<<"Yogesh\n";
//closing
the file
out_obj.close();
out_obj.open("dept.dat");
//writing
the data to the file
out_obj<<"Accounts\n";
out_obj<<"Proof\n";
out_obj<<"Marketing\n";
out_obj<<"DTP\n";
out_obj<<"Graphics Design\n";
out_obj.close();
//Reading
from the files
char Data[80];
ifstream in_obj;
in_obj.open("emp.dat");
cout<<"\n Following are the contents of emp.dat
file...\n";
while(in_obj)
{
in_obj.getline(Data,80);
cout<<"\n"<<Data;
}
in_obj.close();
in_obj.open("dept.dat");
cout<<"\n Following are the contents of dept.dat
file...\n";
while(in_obj)
{
in_obj.getline(Data,80);
cout<<"\n"<<Data;
}
in_obj.close();
getch();
}
Following are the
contents of emp.dat file...

Rahul
Lekhana
Nandan
Archana
Yogesh
Following are the
contents of dept.dat file...

Accounts
Proof
Marketing
DTP
Graphics Design
Write a
program which copies the content of one file to a new file by removing
unnecessary spaces between words.
Solution :
#include<iostream>
#include <fstream>
#include<cstdlib>
using namespace std;
void main()
{
ifstream infile;
ofstream outfile;
infile.open("Sample.dat");
outfile.open("Myoutput.dat");
char output;
if(infile==NULL)
{
cout<<"Error"<<endl;
exit(0);
}
while(infile)
{
infile.get(output);
if(output!='')
{
outfile<<output;
}
}
infile.close();
outfile.close();
ifstream fp;
fp.open("Myoutput.dat");
cout<<"\n Following are the contents of Myoutput.dat
file...\n";
while(fp)
{
fp.get(output);
cout.put(output);
}
fp.close();
}
<Sample.dat>
Statement 1 Statement 2 Statement 3
Following are the
contents of Myoutput.dat file...
Statement1Statement2Statement3
Example:2
Using
file handling methods of C++ write a program and explain how to merge the
contents of two files into one file.
Solution:
Following is a simple C++ program in which we have created two files namely ‒
emp.dat and dept.dat. The contents of both the files are read and merged into
the third file named Combined.dat.
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
void main()
{
//Reading from the files
char Data[80];
ifstream in_obj;
in_obj.open("emp.dat");
ofstream out_obj;
out_obj.open("Combined.dat");
while(in_obj)
{
in_obj.getline(Data,80);
out_obj<<Data;
out_obj<<"\n";
}
cout<<"\n The contents of emp.dat file are
written...\n";
in_obj.close();
in_obj.open("dept.dat");
cout<<"\n The contents of dept.dat file are
written...\n";
while(in_obj)
{
in_obj.getline(Data,80);
out_obj<<Data;
out_obj<<"\n";
}
in_obj.close();
out_obj.close();
ifstream fp;
fp.open("Combined.dat");
cout<<"\n Following are the contents of Combined.dat
file...\n";
while(fp)
{
fp.getline(Data,80);
cout<<"\n"<<Data;
}
fp.close();
}
In
above program, initially the emp.dat
file is opened in a read mode and the Combined.dat
is opened in writing mode. The contents are read from the emp.dat and written
in the Combined.dat. Similarly, the dept.dat file is opened in read mode
and written in Combined.dat. All
these files are then closed. Finally only Combined.dat
file is opened in read mode and the merged contents are displayed on the
console.
Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Handling Multiple Files
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation