Answer to Question #180593 in C++ for Aakash Anand

Question #180593

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.






1
Expert's answer
2021-04-12T08:51:36-0400


#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main(){
	const string FILE_NAME_COLLAGE="college.txt";
	const string FILE_NAME_UNIVERSITY="university.txt";


	ofstream collegeFile(FILE_NAME_COLLAGE, ios::out);
	//Read names of 3 colleges from console
	for(int i=0; i<3; ++i)
	{
		string collegeName;
		cout<<"Enter college name: ";
		getline(cin, collegeName);
		//save college name into the file "college.txt"
		collegeFile << collegeName<<"\n";
	}
	collegeFile.close();


	ifstream collegeFileReading(FILE_NAME_COLLAGE);
	//Copy contents of "college.txt" in another file "university.txt".
	if(!collegeFileReading){
		cerr << "Error: the file "<<FILE_NAME_COLLAGE<<" does not exist.\n";
		return 1;
	}
	string line;
	ofstream universityFile(FILE_NAME_UNIVERSITY, ios::out);
	while(getline(collegeFileReading, line)){
		universityFile << line<<"\n";
    }    
	collegeFileReading.close();
	universityFile.close();


	ifstream universityFileReading(FILE_NAME_UNIVERSITY);
	// open the "university.txt" file and display the following (underlined) characters on console using file.
	if(!universityFileReading){
		cerr << "Error: the file "<<FILE_NAME_UNIVERSITY<<" does not exist.\n";
		return 1;
	}
	cout<<"\n";
	while(getline(universityFileReading, line)){
		cout<< line<<"\n";
		cout<<"________________________________________________\n";
    }   
	universityFileReading.close();


	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS