lseek

来源:互联网 发布:开源中国app源码下载 编辑:程序博客网 时间:2024/05/21 14:10
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH

char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";

int main(void)
{
    pid_t fd;
    
    //creat file if do not exit
    if((fd = open("file.hole",O_RDWR|O_CREAT|O_TRUNC,FILE_MODE)) < 0)
    {
        printf("creat error\n");
        exit(1);
    }
    
    if(write(fd,buf1,10) != 10)
        printf("buf1 write error\n");
    
    if(lseek(fd,40,SEEK_SET) == -1)
        printf("lseek error\n");
    
    if(write(fd,buf2,10) != 10)
        printf("buf2 write error\n");

    exit(0);    

}


$ 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
0000040  \0  \0  \0  \0  \0  \0  \0  \0   A   B   C   D   E   F   G   H
0000060   I   J
0000062



原创粉丝点击