程序3-2 创建一个具有空洞的文件

/*
 ============================================================================
 Name        : test.c
 Author      : blank
 Version     :
 Copyright   : Your copyright notice
 Description : 程序3-2 创建一个具有空洞的文件
 ============================================================================
 */
/*
 *
 */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "ourhdr.h"

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

int main(int argc, char *argv[])
{
	int    fd;
	int    n;
	if ((fd = creat("file.hole", FILE_MODE)) < 0){
		err_sys("create file.hole error\n");
	}
	/*
	 * offset now = 10
	 */
	if (write(fd, buf1, 10) != 10){
		err_sys("write buf1 to file.hole error\n");
	}

	/*
	 * offset now = 16384
	 */
	if (lseek(fd, 16384, SEEK_SET) == -1){
		err_sys("lseek file.hole error\n");
	}

	/*
	 * offset now = 16394
	 */
	if (write(fd, buf2, 10) != 10){
		err_sys("write buf2 to file.hole error\n");
	}

	exit(0);
}

 

posted @ 2014-04-24 23:02  blankqdb  阅读(1118)  评论(0编辑  收藏  举报