File tools

How do I 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.

2. File Display Program.
Write a program that asks the user for the name of a file. The program should display the contents of the file on the screen. If the file's contents won't fit on a single screen, the program should display 24 lines of output at a time, and then pause. Each time the program pauses it should wait for the user to strike a key before the next 24 lines are displayed.

3. File "TAIL" Program:
Write a program that asks the user for the name of a file. The program should then display the last 15 lines of the file. If the file has less than 15 lines, the entire file should be displayed, with a message indicating the entire file has been displayed.


My professor had given me this homework but this seems so confusing to me and i don't know what am I supposed to do? Please, someone, guide me through.
Didin't he/she teach you how to use string, getline, ifstream, loop ?
Hello flamminghot,

Break your program down into smaller steps:
How do I 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.


This tells me that the program will start be asking the user for a file name. With this you can start a program with the header files that you will need then inside main define some variables that you will need then ask the user for the file name. I have a function I use that will list the file names you might need based on the extension of the file name. I like using this so the user knows how to spell the file correctly. Once you have the file name I would work on opening the file and making sure that works.

My thought nest would be once the file is open read the entire file until the end and put each line read into a vector of strings. If you do not know about vectors yet an array of strings would work, but a vector is better as it only holds what you need whereas an array would need to be bigger than what you would need and some of the array may be unused space.

Doing it this in steps 1 - 3 you can use the part of the vector that you need.

Once you get this part working adding steps 2 and 3 will be much easier.

When working with files I have found this code that I wrote to be useful for people that are new to working with "fstream" and files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string iFileName{ "" };  // <--- Put file name here. Or in your case the variable that holds the file name.

std::ifstream inFile;

inFile.open(iFileName);

if (inFile.is_open())
{
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
	exit(1);
}

Lines 9 and 10 I comment out when I no longer need them. As you gain a better understanding if using files this code can be shortened. When you figure that out you are doing better and have learned something.

Hope that helps,

Andy
Hello flamminghot,

When I started writing the program I was trying to put all three steps into one program when I figured out that it is three separate programs.

The first two are easy to figure out. The third is a little harder only printing the last fifteen lines of an unknown file. I used a vector of strings to hold each sentence of the file and the math was easy to set up a for loop to print the last fifteen lines. Another way would be to read the file to count the lines then use count minus fifteen to start the for loop. Reopen the file and skip everything except the last fifteen lines.

Hope that helps,

Andy
@Andy thanks a lot for breaking down the question for me. This made me a lot simpler for me to understand.

@Thomas. She taught us in the class. But i couldn't understand. It was my fault though.
Hello flamminghot,

In the future pick apart your instructions into smaller parts and then you can work on one part at a time instead of trying to figure out the whole program at one time.

Write some code an post it. I will be glad to help you through it. There is a good chance that I can help you understand better than your teacher who is trying to teach the course in a short period of time where as I am not.

Andy
Topic archived. No new replies allowed.