Answer to Question #180614 in C++ for Yedavilli Naga Prerna

Question #180614

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


1
Expert's answer
2021-04-12T17:29:49-0400
#include<iostream>
#include <fstream>
#include<string>
#define startunder "\033[4m"
#define finishunder "\033[0m"
using namespace std;


int main()
{
    // creating and writing data to a file college.txt
    ofstream file1("college.txt");
    if (!file1)
    {
        cout << "Error opening file program terminated" << endl;
        return 1;
    }
    file1 << "Lovely Professional University" << endl;
    file1 << "Thapar Institute of Engineering and Technology" << endl;
    file1 << "Dr B R Ambedkar National Institute of Technology" << endl;
    file1.close();
    // copy file college.txt to university.txt
    string line;
    ofstream file2("university.txt");
    if (!file2)
    {
        cout << "Error opening file program terminated" << endl;
        return 1;
    }
    ifstream file3("college.txt");
    if (!file3)
    {
        cout << "Error opening file program terminated" << endl;
        return 1;
    }

    while (getline(file3, line))
    {
        file2 << line << endl;
    }
        file3.close();
        file1.close();
   // output underlined file
        ifstream file4("university.txt");
        if (!file4)
        {
            cout << "Error opening file program terminated" << endl;
            return 1;
        }
        while(getline(file4, line))
        {
       cout << startunder << line << endl << finishunder;
        }
        file4.close();
        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