在apue第65页有一个用lseek函数制造文件空洞的程序,贴在下面:
#include "apue.h"
#include <fcntl.h>
char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";
int main(void){
int fd;
if((fd = creat("file.hole", FILE_MODE)) <0)
err_sys("creat error");
if(write(fd, buf1, 10)!=10)
err_sys("buf1 write error");
if(lseek(fd, 16384, SEEK_SET) == -1)
err_sys("lseek error");
if(write(fd, buf2, 10)!=10)
err_sys("buf2 write error");
exit(0);
}
lseek指针往后偏移16384个字节,里面都是'\0'.:
linux@linux:~/apue/c4$ od -c ../c3/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
*