Linux程序设计:lseek函数

目录

lseek函数

lseek函数应用案例


lseek函数

#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
  • 返回: 若成功则返回新的文件位移量(绝对偏移量,相对于文件开始位置的偏移量),若出错为-1
  • 功能: 定位一个已打开的文件
  • 对应的C库函数
int fseek(FILE *stream,long offset,int whence);
  • lseek函数参数
fd
  • 已打开文件的文件描述符
offset
  • 位移量(相对于whence的偏移量)
whence
  • 定位的位置:
SEEK_SET
  • 将该文件的位移量设置为距文件开始处 offset个字节。
SEEK_CUR
  • 将该文件的位移量设置为其当前值加offset,offset可为正或负。
SEEK_END
  • 将该文件的位移量设置为文件长度加offset,offset可为正或负。
  • lseek也可用来确定所涉及的文件是否可以设置位移量。
  • 如果文件描述符引用的是一个管道或FIFO(这两种文件不支持lseek),则lseek返回-1,并将errno设置为EPIPE。
  • 每个打开文件(只有打开的文件,才有文件偏移量的概念,所以可知文件偏移量是不记录在文件属性中的
    )都有一个与其相关联的"当前文件偏移量"。
    • 它是一个非负整数,用以度量从文件开始处计算的字节数
    • 通常,读、写操作都从当前文件偏移量处开始,并使偏移量增加所读或写的字节数。
    • 按系统默认,当打开一个文件时,除非指定O_APPEND选择项,否则该位移量被设置为0。

lseek函数应用案例

  • src文件夹下copy.c
#include "io.h"
#include <fcntl.h>
#include <unistd. h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, char * argv[])
{
	if (argc !=3){
		fprintf(stderr, "usage: %s srcfile destfile\n", argv[0]); 
		exit(1);
	}
    //打开一个待读取的文件
    fdin = open(argv[1], O_RDONLY);
    //计算文件长度
    printf("file length: ld%\n", lseek(fdin. 0L, SEEK_END));
    //将指针移向头部
    lseek(fdin, 0, SEEK_SET);
    if(fdin < 0){
	    fprintf(stderr, "open error:%s\n", strerr(errno));
	    exit(1);
	}
	else {
	    printf("open file:%d\n",fdin);
}
    //打开一个待写入的文件
	fdout = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0777);
	if(fdout < 0){
	    fprintf(stderr,"open error:%s\n", strerror(errno));
	    exit(1);
    else {
	    printf("open file:%d\n",fdin);
    }
}
	//文件复制
	copy(fdin, fdout);
	close(fdin);
	close(fdout);
    return 0;
}
  • src文件夹下io.c
#include "io.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
#define BUFFER_LEN 1024
void copy(int fdin, int fdout)
{
    char buffer[BUFFER_LEN];
    ssize_t size;
    while((size = read(fdin, buffer,  BUFFER_LEN)]) > 0){
    //当前偏移量位置
    printf("current: ld%\n", lseek(fdin, 0L, SEEK_CUR));
        if(write(fdout, buffer, size)!= size){
            fprintf(stderr, "write error: %s\n", strerror(errno)); 
            exit(1);
            }
    }
    if (size <0){
        fprintf(stderr, "read error: %s\n", strerror(errno));
        }
        exit(1);
    }
}
  • 案例二:生成空洞文件
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>

/*生成空洞文件*/
char* buffer = "0123456789";
int main(int argc, har* rgv[]){
	if(argc < 2){
	    fprintf(stderr, "-usage:%s [file]\n", rgv[0]);
	    exit(1);
	}
	int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0777); 
	if(fd <0)
		perror("open error"); 
		exit(1);
	}
	size_t size = strlen (buffer)* sizeof(char); 
	if(write(fd, buffer, size)!= size){
		perror("write error"); 
		exit(1);
	}
	//定位文件尾部的10个字节处
	if(lseek(fd, 10L, SEEK_END) < 0){
	perror("Iseek error");
	exit(1);
	}
	//从文件尾部的10个字节处再写入字符串
	if(write(fd, buffer, size)!= size){
		perror("write error"); 
		exit(1);
	}
    close(fd);
	return 0;
}

  • 演示结果:

  • 小结

猜你喜欢

转载自blog.csdn.net/baidu_41388533/article/details/108395769