In: Computer Science
ope the template will help here. Assume you already open the punchline file as 'infile'.
char
ch;
// To hold a character
string line;
// To hold a
line of input
bool foundLastLine = false; // Flag, intitalized to
false
// Find the beginning of the last line
// Clear the eof bit just in case we're
// already at end of the file.
infile.clear();
// Go to the end of the file.
infile.seekg(0, ios::end);
// Move backwards to beginning of the
// final char in the file and read it in
infile.seekg(-1, ios::cur);
ch = infile.get();
// Back up until we find a newline.
while (infile && !foundLastLine)
{
// If the char just read is NOT a
newline...
if.....
{
// Back up two
more characters (one
// to skip the
char just read and one
// more to move
just before the previous
// char so we can
read it next.
.............
.............
}
else
{
// We found the
newline at the end of the
// next to last
line, so we are where we
// want to be. The
next char begins the
// final
line.
foundLastLine =
true;
}
}
// Read final line and display it
getline(infile, line, '\n');
cout << line;
C++
I used ifstream headerfile to read the file which supports seekg() function.
seekg() is used to move the location by some characters from the stated position.
if we used infile.seekg(0,ios::end) then it will 1st move the end of the file and from there it will move 0 characters from eof it's stays in the same position.
if we use seekg(10,std::beg) -> then it will 1st go the start of the file and from there it will move 10 characters in forward direction.
here offset is positive then moves forward, if negative moves backward.
So, we will move some -i characters backward for each character reading.
Since we read the last character in infile.seekg(-1,ios::end) we will move the location to 2nd position from end i.e -2 after reading second file we will read 3rd character by moving 3 locations backward i.e -3 like this we will keep on increasing i value and move backwards by 1 character.
What if there is only 1 line in the file?
Here we won't get '\n' character in file how it terminates then. For that we will get the size of the file and we will check whether the value of i is greater than the size of the file or not. If i is greater then we will move to the beginning of file and breaks the loop.
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
ifstream
infile("C:\\Users\\S\\Desktop\\data.txt",std::ios::ate);
char ch; // To hold a character
string line; // To hold a line of input
bool foundLastLine = false;
infile.clear();
// Go to the end of the file.
infile.seekg(0, ios::end);
// Move backwards to beginning of the
// final char in the file and read it in
infile.seekg(-1, ios::cur);
ch = infile.get();
// Back up until we find a newline.
std::streampos size = infile.tellg();
//cout<<size;
int i=2;
while (infile && !foundLastLine)
{
// If the char just read is NOT a newline...
if(ch!='\n')
{
if(i>size){
infile.seekg(0,ios::beg);
break;
}
infile.seekg(-i,std::ios::end);
ch=infile.get();
//cout<<ch;
++i;
}
else
{
foundLastLine = true;
}
}
// Read final line and display it
getline(infile, line, '\n');
cout << line;
}
Sample Run:
input file:
In addition to good(), which checks whether the stream is ready for input/output operations
Output:
In addition to good(), which checks whether the stream is ready for input/output operations
input file:
Using this prototype, the position of
the get or put pointer is set to an offset value relative to
some
specific point determined by the parameter direction. offset is of
the member
type off_type, which is also an integer type. And direction is of
type seekdir, which is an enumerated type (enum) that
determines
the point from where offset is counted from, and that can take any
of the following values
Output:
the point from where offset is counted from, and that can take any of the following values