Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Write a program that asks the user for the name of a file. The program shoul

ID: 3673970 • Letter: C

Question

C++

Write a program that asks the user for the name of a file. The program should display the first 10 lines of the file on the screen (the “head” of the file). If the file has fewer than 10 lines, the entire file should be displayed, with a message indicating the entire

file has been displayed.

The function runs from the command line but can run from visual studio using the “more” option. Call the program YourNameDisplay

1) YourNameDisplay filename will by default perform the “more” operations (item #2).

2) YourNameDisplay –h filename will by default perform the “head” operation (item #1) and display 10 lines

3) YourNameDisplay –h n filename will perform the “head” operation for n lines (e.g. YourNameDisplay –h 30 myFile.txt will list the first 30 lines of the file)

4) YourNameDisplay –t filename will by default perform the “tail” operation (item #4) and display the last 10 lines

5) YourNameDisplay –t n filename will perform the “tail” operation for n lines (e.g. YourNameDisplay –t 20 myFile.txt will list the last 20 lines of the file)

6) YourNameDisplay -? Will print out directions for running the file from the command prompt.

Explanation / Answer

/*File header program

Write a program that asks the user for the name of a file. The program should display the first 10 lines

of the file on the screen (the "head" of the file). If the file has fewer than 10 lines, the entire file should be displayed,

with a message indicating the entire file has been displayed */

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

int main()

{

string fileName,

                   input;

int i;

char ch;

fstream file;

//Get the file name

cout<< "Enter a file name: ";

cin >> fileName;

//Open the file

file.open(fileName, ios::in);

//If the file was successfully openeed, continue

if (file)

{

                //get a character from the file

                getline(file,input);

                string tempinput=input;

               

                for(int i=0; i<10; i++)

                {

                                cout << input;

                                getline(file, input);

               

               

                                if (tempinput==input)

                {

                                cout <<" "<<endl;

                                break;

                               

                                }

                               

                }

                file.close();

                cout<<" The first 10 lines have been displayed"<<endl;

}

else

cout<<fileName<< "could not be opened. ";

cin.get();

cin.get();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote