Professional Documents
Culture Documents
Roll no.34
Experiment No 12
Aim: Write a C++ program for File Handling which reads and writes the contents of two different files
and display the content of the file in output screen.
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: name.dat and surname.dat Through our C++ program we are writing some contents to them.
Later on reading those contents and displaying them on the console.
#include<iostream>
#include<fstream.h>
int main()
ofstream o1;
o1.open("name.dat");
o1<<"Rahul\n";
o1<<"Sachin\n";
o1.close();
o1.open("surname.dat");
o1<<"Dravid\n";
o1<<"Tendulkar\n";
o1.close();
char Data[80];
ifstream i1;
i1.open("name.dat");
while(i1)
i1.getline(Data,80);
cout<<"\n"<<Data;
i1.close();
i1.open("surname.dat");
cout<<"\n Followint are the contents of the surname.dat file ..... \n";
while(i1)
i1.getline(Data,80);
cout<<"\n"<<Data;
i1.close();
return 0;