Programming

C++ Program to Read and Write Files Using Multiple File Handling

Create a.txt file “districts” which contains the five different districts, create another file headquarter containing the headquarters of file district. WAP to read and write the files using concept of multiple file handling.

Source Code

#include<iostream>

#include<fstream>

#include<conio.h>

using namespace std;

int main()

{

char buffer[100];

int maxlength=100;

ofstream outf;

outf.open("district.txt");

{

    outf<<"| Kathmandu | Bhaktapur | Lalitpur | Dhading | Illam |";

}

outf.close();

outf.open("headquarter.txt");

{

    outf<<"| Kathmandu \t| Bhaktapur \t| Patan \t| Dhading\t| Taplejung \t|";

}

outf.close();

ifstream fin;

fin.open("district.txt");

while(fin)

{

    fin.getline(buffer,maxlength);

    cout<<"\n"<<buffer;

}

fin.close();

fin.open("headquarter.txt");

while(fin)

{

    fin.getline(buffer,maxlength);

    cout<<"\n"<<buffer;

}

fin.close();

getch();

}

Output

Multiple File Handling

 

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.