Click here to Skip to main content
14,930,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Implement a program that displays the last 'n' lines of a file to console, where 'n' is input from user c++. Hints: - Don't read the whole file. - Don't use getline

What I have tried:

answer at the question by c++
file organization
Updated 29-Apr-21 1:49am

Make a rough estimate of the number of bytes you need and then read them.
After reading, process the data in order to verify you have got enough lines.
If you have indeed enough lines then print the required ones.
On the other hand, if you have not enough lines then revise your initial estimate and try again.
Member 15176306 29-Apr-21 8:58am
coding please
CPallini 29-Apr-21 8:59am
'Coding' is up to you.
Greg Utas 29-Apr-21 13:12pm
You need to do this yourself. This doesn't seem like a first assignment in C++, in which case you already have some experience.

Note that seekg allows you to go to the *end* of a file. From there, you should be able to back up over endlines and...

#include <fstream>  // defines ifstream
Documentation for what's available in ifstream, including seekg, is here[^]. That's my go-to site for looking up almost anything related to C++.
using namespace std ;
int main()

Rick York 29-Apr-21 11:26am
This is your solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900