开发者

How to use lseek to read the last character of a file

开发者 https://www.devze.com 2023-02-05 05:12 出处:网络
I\'m trying to read the char开发者_如何学Goacters from a file in reverse order using lseek. So far, I have:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消