lseek()

来源:互联网 发布:北外网络教育登录 编辑:程序博客网 时间:2024/06/06 13:11

#include <sys/types.h>

#include <unistd.h>

off_t   lseek(int  fd,  off_t  offset,  int whence);

lseek(fd,100,SEEK_CUR);

whence:

SEEK_SET:文件指针偏移的时候相对位置是文件开始

SEEK_CUR:文件指针偏移的时候相对位置是当前移动到的文件指针(就是指写入空格)

SEEK_END:文件指针偏移的时候相对位置是文件结束

offset:偏移量

off_t:*_t是整数的别名 typedef定义的数据类型


例子:

int main()

{

int fd=0;

fd=open("1.log",O_RDWR|O_CREAT|O_TRUNC,0644);

if(fd<0)

{

perror("open");

exit(0);

}

 

//写文件

char buf[32]={0};

strcpy(buf,"hello");

write(fd,buf,strlen(buf));


//移动文件指针

lseek(fd,100,SEEK_CUR);

strcpy(buf,"world");

write(fd,buf,strlen(buf));


//关闭文件

close(fd);

return 0;

}



0 0
原创粉丝点击