首页 > 系统服务 > 详细

linux文件空洞

时间:2015-06-28 23:07:27      阅读:440      评论:0      收藏:0      [点我收藏+]
#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页!!!

linux文件空洞

原文:http://blog.csdn.net/h1023417614/article/details/46674297

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!