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


Expert's answer

#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;
}

LATEST TUTORIALS
APPROVED BY CLIENTS