linux读写文件实例,Linux下大文件读写源码示例

本文档展示了如何在C语言中使用LARGEFILE_SOURCE宏处理大文件,通过定义off_t的64位表示,演示了open()函数的O_LARGEFILE选项,并进行了off_t值的读写和文件大小的统计操作。
摘要由CSDN通过智能技术生成

#define _LARGEFILE_SOURCE

#define _LARGEFILE64_SOURCE

#define _FILE_OFFSET_BITS   64

#include

#include

#include

#include

#include

int main()

{

printf("sizeof(off_t) = %d\n", sizeof(off_t));

int fd = 0;

#if 1

fd = open("/root/test", O_WRONLY | O_CREAT | O_LARGEFILE, 0600);

#else

fd = open("/dev/hda1", O_RDONLY, 0600);

#endif

if (fd > 0)

{

off_t l = 0x200000001LL;

off_t l_new = lseek(fd, l, SEEK_SET);

if (l_new < 0)

printf("l_new =%d\n", l_new);

#if 1

else

write(fd, &fd, sizeof(fd));

#endif

printf("l_new = 0x%.16x\n", l_new);

close(fd);

}

struct stat st;

int ret = stat( "/root/test", &st);

printf( "\n%d--%I64u-\n", ret, st.st_size );

}0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值