linux文件空洞

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char const *argv[])
{

char *buf[1024];
int recfile=open("./file",O_RDWR | O_CREAT,0644);
    if (recfile < 0) {
    perror("open hello");
    exit(1);
    }

    if (write(recfile,"1232",4)<0)
    {
        perror("open hello");
        exit(1);
    }
    if(lseek(recfile,15,SEEK_SET) < 0)
    {
        perror("open hello");
        exit(1);
    }

    if (write(recfile,"99\0",3)<0)
    {
        perror("open hello");
        exit(1);
    }

    if(lseek(recfile,0,SEEK_SET) < 0)
    {
        perror("open hello");
        exit(1);
    }

read(recfile,buf,1024);
printf("%s     ",buf);
printf("%s",buf+15);
    return 0;
}
//(0)输出1232     99

/*
(1)ls -l file    ->18字节
(2)cat file ->123299
(3)
sublime file ->
3132 3332 0000 0000 0000 0000 0000 0039
3900
(4)od -tcx file ->
1232 \0\0\0\0 \0\0\0\0 \0\0\0 9 9 \0

*/

/*
结论:
(0):文件的文件结束符EOF并不是空字符0
(1):ls 显示的是文件的size
(2):cat会去除文件的空字符
(3):空洞被设置为0


额外:如果在尾端lseek了空洞后,没有执行写操作,那么文件大小不会改变。
*/

参考unix环境高级编程第三版 54页和90页!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫云的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值