当需要在大于2G的文件中跳转或在更大的块设备中跳转的时候lseek是无法完成任务的,这需要使用其他的文件跳转系统调用。
LINUX中有系统调用llseek,用他可以实现64位的跳转,完全可以支持现在最大的文件或块文件的大小。

#include <sys/types.h>
#include <unistd.h>

int _llseek(unsigned int fd, unsigned long offset_high,unsigned long offset_low, loff_t *result,unsigned int whence);

而lseek64是_llseek的包装函数

  #define _LARGEFILE64_SOURCE     
  /* See feature_test_macros(7) */
  #include <sys/types.h>
  #include <unistd.h>

  off64_t lseek64(int fd, off64_t offset, int whence);

用lseek64可以简便的在代码中完成跳转。不过在使用前需要定义宏_LARGEFILE64_SOURCE

注意

当对大于2G的文件或块设备文件操作时,open打开时flag上需要加上O_LARGEFILE 表示以大文件的方式打开。

添加O_LARGEFILE时编译时会出现错误

error: O_LARGEFILE undeclared (first use in this function)

解决的办法是添加宏

#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* for O_DIRECT */
#endif

或者

#define __USE_FILE_OFFSET64
#define __USE_LARGEFILE64
#define _LARGEFILE64_SOURCE

注意宏添加在头文件前,不然依然报错

GitHub 加速计划 / li / linux-dash
13
2
下载
A beautiful web dashboard for Linux
最近提交(Master分支:3 个月前 )
186a802e added ecosystem file for PM2 5 年前
5def40a3 Add host customization support for the NodeJS version 5 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐