You are on page 1of 3

Name:Mohit Datir

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.

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: 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>

using namespace std;

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();

// reading from the files

char Data[80];

ifstream i1;
i1.open("name.dat");

cout<<"\n Following are the contents of name.dat file........\n";

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;

You might also like