Linux系统调用四、lseek()函数详解



❀1. 案例:写文件并把写入内容打屏

我们可以通过上一节所讲的read()和write()函数来实现向一个文件中写入内容并把写入内容打印到屏幕的功能。

/************************************************************
  >File Name  : readandprint.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月13日 星期五 12时11分44秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define BUF_MAX 512 /*buf缓冲区最大值*/

/*向中文件写入数据并把写入内容打印到标准输出*/
int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("not fount file name");
		return -1;	
	}
	int fd = open(argv[1], O_RDWR | O_CREAT);
	write(fd, "hello linux...", 15);
	char buf[20];
	memset(buf, 0, sizeof(buf));
	int read_size = read(fd, buf, sizeof(buf));
	if(read_size > 0)
	{
		write(STDOUT_FILENO, buf, read_size);	/*STDIN_FILENO STDERR_FILENO*/
	}
	close(fd);
	return 0;
}

我们知道,在C语言中,字符串都是以 ‘’ 结尾的,比如 “hello linux…” 加上结束符共15字节。

write(fd, "hello linux...", 15);

我们来测试下程序,首先明确一点,字符串会写入相应文件,但是不会打印在屏幕中,这个后面分析。这里先看一下结束符 ‘’ 是如何显示的。

可以看到,确实不会打屏,且文件内容已写入。我们通过vim编辑器打开1.txt文件。
在这里插入图片描述
可以看到一个 ‘^@’ 字符,这个就是我们多写入的 ‘’ 字符,如果我们把写入字节数15改为14,就没有这个字符了。

❀2. lseek移动文件读写位置

  • 包含头文件
#include <sys/types.h>
#include <unistd.h>
  • 函数原型
off_t lseek(int fd, off_t offset, int whence);
  • 函数功能

    reposition read/write file offset.

  • 函数参数

    • fd:文件描述符
    • offset:偏移量
    • whence:位置
      • SEEK_SET:The offset is set to offset bytes. offset为0时表示文件开始位置。
      • SEEK_CUR:The offset is set to its current location plus offset bytes. offset为0时表示当前位置。
      • SEEK_END:The offset is set to the size of the file plus offset bytes. offset为0时表示结尾位置
  • 函数返回值

    • 成功返回当前位置到开始的长度
    • 失败返回-1并设置errno

下面我们通过上面的案例来分析lseek函数的用法,上面案例测试中说到,字符串已经写入了相应文件,但是并没有打印在屏幕中。这是因为,我们用write()函数写入文件之后,这时候读写位置就指在写完后的那个位置,也就是字符串的后面,这样我们在使用read()函数去读的时候就相当于从写入字符串的后面去读的,所以啥也没读到。这时候,就可以使用lseek()函数来移动读写位置,我们只需在上面代码中加一句话即可。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define BUF_MAX 512 /*buf缓冲区最大值*/

/*向中文件写入数据并把写入内容打印到标准输出*/
int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("not fount file name");
		return -1;	
	}
	int fd = open(argv[1], O_RDWR | O_CREAT);
	write(fd, "hello linux...", 15);
    /*读写位置在末尾*/
    /*把读写位置移动到文件首部*/
    lseek(fd, 0, SEEK_SET);
	char buf[20];
	memset(buf, 0, sizeof(buf));
	int read_size = read(fd, buf, sizeof(buf));
	if(read_size > 0)
	{
		write(STDOUT_FILENO, buf, read_size);	/*STDIN_FILENO STDERR_FILENO*/
	}
	close(fd);
	return 0;
}

再测试一下,就发现可以正常打屏了。
在这里插入图片描述

❀3. lseek计算文件大小

利用lseek()函数执行成功时的返回值可以来计算一个文件所占字节的大小。

/************************************************************
  >File Name  : getsize.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月13日 星期五 18时47分04秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("not found filenamen");
		return -1;	
	}
	int fd = open(argv[1], O_RDONLY);
	int size = lseek(fd, 0, SEEK_END);
	printf("file size: %dn", size);
	close(fd);
	return 0;
}

运行程序测试结果如下。
在这里插入图片描述

❀4. lseek拓展文件大小

我们知道lseek()函数有三个参数,在前面的案例中,都把第二个参数偏移量offset设置为0来处理的,这样第三个参数就不用加偏移量了,相当于whence位置都是相对于文件首部来计算的。如果我们使用第二个参数offset,并把位置whence设置为文件尾,就相当于在文件尾再偏移offset个字节,这就达到了扩展文件大小的目的。

/************************************************************
  >File Name  : expandfile.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月13日 星期五 19时02分06秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		perror("not found filename: ");
		return -1;	
	}
	int fd = open(argv[1], O_WRONLY);
	lseek(fd, 10, SEEK_END);
	close(fd);
	return 0;
}

编译并运行,然后查看文件大小是否增加。
在这里插入图片描述
通过对比我们发现,文件大小并未增加。这是因为通过lseek()扩展了文件的大小之后,如果我们没有对该文件进行写操作,那么这个扩展的内容默认是不会保存的,所以文件大小不会改变。所以,在扩展后,至少要对文件写一次才能保存,我们对上面程序增加一个写操作,然后进行测试。

int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		perror("not found filename: ");
		return -1;	
	}
	int fd = open(argv[1], O_WRONLY);
	lseek(fd, 10, SEEK_END);
    write(fd, "a", 1);
	close(fd);
	return 0;
}

在这里插入图片描述
我们运行后发现,文件大小从0变成了11,扩展了11个字节,而我们程序中仅指定扩展了10个字节,这是因为我们扩展完后又写入了一个字节a,通过前面的分析我们知道,在lseek()函数执行完毕后,读写位置应该是在文件尾部,这时再写入一个字符就相当于在文件尾部,也就是第11个字节出写入了一个a,保存后最终大小为11字节。我们可以使用vim打开文件查看一下。
在这里插入图片描述

可以看到10个 ‘^@’ 字符,第11个字符为写入的 ‘a’ 。

注:这里用到了一个函数叫做perror(),这个函数是用来打印错误信息的,我们在上面这些函数的返回值都可以看到一条,如果出错会设置errno,而设置errno就是和perror()函数相关联的,通过perror()这个函数就可以把出错信息打印出来。


在这里插入图片描述


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>