c++ - 如何使用lseek读取文件的最后一个字符

标签 c++ unix system-calls

我正在尝试使用 lseek 以相反的顺序从文件中读取字符。

到目前为止,我有:

 int finished = 1;
 char temp[1];

    while (finished > 0) {

 lseek(fileID,0,2);

 finished = read(fileID, &temp, 1);

 cout << temp[0];

    }

但读取总是返回 0。

有什么想法吗?

最佳答案

当然应该调用 lseek()

lseek(fileID, -1, SEEK_END);

您正在寻找文件末尾,您需要少一个字节。

关于c++ - 如何使用lseek读取文件的最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4732592/

相关文章:

c++ - 如何在命令行界面中处理 ctrl-break 信号

linux - 任何人都可以向我解释/dev/tty 的目的是什么

c - 警告 : incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

c - I/O系统调用

c - 为什么 fseek 使用 read() 系统调用?

c++ - 更新数组元素(数组中有新元素进入后,最旧的元素将被删除)

c++ - 在 objective-c 中使用 ARC 转换 C++ 类型

c++ - if constexpr std::is_same 在 VS 2022 下

python - conda 和 python shell 脚本

linux - 连接 :Direct From Linux server to Unix