How to use lseek to read the last character of a file
I'm trying to read the char开发者_如何学Goacters from a file in reverse order using lseek.
So far, I have:
int finished = 1;
char temp[1];
while (finished > 0) {
lseek(fileID,0,2);
finished = read(fileID, &temp, 1);
cout << temp[0];
}
But read always returns 0.
Any ideas on what to do?
Surely the call to lseek() should be
lseek(fileID, -1, SEEK_END);
You are seeking to the end of file and you need to be one byte short.
精彩评论