//off_t lseek(int fd,off_t offset, int base) 偏移量 搜索的起始位置(文件头(SEEK_SET),当前指针位置(SEEK_CUR),文件尾(SEEK_END))unistd.h
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<errno.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#define ERR_EXIT(m) (perror(m),exit(EXIT_FAILURE))
#define ERR_EXIT(m)
do
{
perror(m);
exit(EXIT_FAILURE);
}while(0) //宏要求一条语句
int main(void)
{
int fd;
fd=open("hole.txt",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(fd==-1)
ERR_EXIT("open error");
//写入5个字符
write(fd,"ABCDE",5);
//将文件偏移
//od -c hole.txt查看文件空洞。实际磁盘上并未存放 这些空洞 ls -lh hole.txt有1.1G但实际磁盘上并未全部存储。
//并未在磁盘中全部存放,很多的 信息未存放在磁盘,只有部分信息。du -h hole.txt(磁盘最小块4K大小)
if(ret==-1)
int ret=lseek(fd,1024*1024*1024/*32*/,SEEK_CUR); //从文件当前位置,偏移到1G之后。只在内核中操作,实际不在磁盘操作。
ERR_EXIT("lseek error");
write(fd,"hello",5);
close(fd);
return 0;
}