// Print the last n lines of a file i.e implement your own tail command
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream rd("D:\\BigFile.txt");
int cnt = 0;char c;
std::string data;
rd.seekg(0,rd.end);
int pos=rd.tellg();
while(1)
{
rd.seekg(--pos,std::ios_base::beg);
rd.get(c);
if(c=='\n')
{
cnt++;
// std::cout<<pos<<"\t"<<rd.tellg()<<"\n";
}
if(cnt==10)
break;
}
rd.seekg(pos+1);
while(std::getline(rd,data))
{
std::cout<<data<<"\n";
}
}
So, I wrote this program to print the last 10 lines of a text file. However it prints only the last 5 , for some reason every time it encounters an actual '\n' the next get() also gives a \n leading to incorrect output . Here is my input file:
Hello
Trello
Capello
Morsello
Odello
Othello
HelloTrello
sdasd
qerrttt
mkoilll
qwertyfe
I am using notepad on Windows and this is my output:
HelloTrello
sdasd
qerrttt
mkoilll
qwertyfe
I cant figure out why this is happening , Please help.