linux file hole

APUE 中的file hole

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#define    FILE_MODE    (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main (void) 
{
    char buf1[]="abcdefghij";
    char buf2[] = "ABCDEFGHJIJ";
    int fd;
    if((fd=creat("file.hole",FILE_MODE))<0)
            printf("create file error!\r\n");
    if(write(fd,buf1,sizeof(buf1)) != sizeof(buf1))
            printf("buf1 write error!\r\n");
    if(lseek(fd,16384,SEEK_SET) == -1) 
            printf("lseek error!");
    if(write(fd,buf2,sizeof(buf2)) != sizeof(buf2))
            printf("buf2 write error!");
    exit(0);

}

结果如下:

jason@ubuntu:~/apue$ od -c file.hole 
0000000   a   b   c   d   e   f   g   h   i   j  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0040000   A   B   C   D   E   F   G   H   J   I   J  \0
0040014

每一行开头的七位数字是十六进制形式的距离文件开头的偏移量,单位是 byte

    原文作者:JasonZhang
    原文地址: https://segmentfault.com/a/1190000008901663
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞