Write a C++ program to create a new file named “college.txt” to store list of colleges under Punjab university. Read names of 3 colleges (consider input given below) from console and store it in “college.txt”. Further copy contents of “college.txt” in another file “university.txt”. Now open the “university.txt” file and display the following (underlined) characters on console using file manipulation functions.
Input:
Lovely Professional University
Thapar Institute of Engineering and Technology
Dr B R Ambedkar National Institute of Technology
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
int main()
{
list<string> colleges;
colleges.push_back("Lovely Professional University");
colleges.push_back("Thapar Institute of Engineering and Technology");
colleges.push_back("Dr B R Ambedkar National Institute of Technology");
fstream fs;
fs.open("colleges.txt", fstream::out);
string buf;
if(fs.is_open())
{
list<string>::iterator iter;
for (iter = colleges.begin(); iter != colleges.end(); ++iter)
{
buf = *iter;
fs << buf;
fs << "\n";
}
}
else
cout << "ERROR OPENNING FILE" << endl;
fs.close();
fstream f_from, f_to;
f_from.open("colleges.txt", fstream::in);
f_to.open("universitys.txt", fstream::out | fstream::app);
if(f_from.is_open() && f_to.is_open())
{
while(!f_from.eof())
{
buf = "";
f_from >> buf;
f_to << buf;
f_to << "\n";
}
cout << "Successfully copied";
}
else
{
cout << "Can't open file(";
exit(0);
}
f_from.close();
f_to.close();
return 0;
}
Comments
Leave a comment