Here is the code which will help me to print the last three lines of a file:
include<iostream>
include<conio.h>
include<fstream>
include<string.h>
using namespace std;
int main() { ifstream f1; f1.open("C:\Users\ARNAB BANERJEE\yoyo.txt"); int no_of_lines=1; int cnt=1; while(no_of_lines<=3) { f1.seekg(-cnt,ios::end);//take the get pointer to the last character in the file char ch=f1.get(); printf("%c",ch); if(ch=='\n') { printf("here"); no_of_lines++; } cnt++; }
return 0;
}
The input file is:
arise\n awake and\n stop not\n till\n the goal\n is\n reached. \n represents newline Output is:
.dehcrea here heresi here
I am unable to understand why two "here"'s are appearing in between two lines. On trying from the start of the file to the end it is giving me correct output..Please do help me!!!!