4.文件—读文件read

  • Post author:
  • Post category:其他


1.基础知识

(1)man 2 read

在这里插入图片描述

fd:指向用open打开的文件

read(fd,buff,count);从fd指向的文件读取count字节的数据放到缓存buff中。

(2)man malloc

发现使用malloc要使用#include<stdlib.h>这个头文件

(3)lseek

作用:将文件读写指针相对whence移动offset个字节

man lseek

在这里插入图片描述
offset是偏移值

whence是位置

whence的3个参数:SEEL_SET指向文件的头;SEEK_END指向文件尾巴;SEEK_CUR指向当前位置

off_t lseek(int fd, off_t offset, int whence);//使用这句代码可以配置指针指向文件的头

lseek()的返回值是偏移量大小(字节)

2.使用read

(1)错误例子

在这里插入图片描述
在这里插入图片描述
问题:读不到任何数据

原因:在执行 int n_write = write( fd,buf,strlen(buf));

后,指针指向字符串末尾。此时指向read,是从内存上的字符串所在空间的后面开始读,读到的内容不是字符串,是空的。

解决办法:关闭文件(close)再重新打开(open);或者把指针移动到字符串首地址。

(2)正确用法

关闭文件(close)再重新打开(open)

在这里插入图片描述

在这里插入图片描述

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
        int fd;
			 char *buf = "yrx0203";
        fd = open("./file1",O_RDWR);
        if(fd == -1)
        {
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0)
                printf("creat file1 success\n");
        }
			printf("open file1 success\n");


//	ssize_t write(int fd, const void *buf, size_t count);
	int n_write = write( fd,buf,strlen(buf));
	if(n_write != -1)
	{
		printf("write %d byte to file1 \n", n_write);
	}


//关闭再重新打开,使指针指向字符串首地址
	close(fd);
	fd = open("./file1",O_RDWR);

	char *readBuf;
	readBuf = (char *)malloc(sizeof(char)*n_write+1);
	//ssize_t read(int fd, void *buf, size_t count);
 	int n_read = read(fd, readBuf, n_write);
	printf("read %d ,contex:%s\n",n_read,readBuf);



	 close(fd);
 	 return 0;
}


把指针移动到字符串首地址(将光标移动到文件的头)。

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

int main()
{
        int fd;
	char *buf = "yrx0203";
        fd = open("./file1",O_RDWR);
        if(fd == -1)
        {
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0)
                printf("creat file1 success\n");
        }
	printf("open file1 success\n");


//	ssize_t write(int fd, const void *buf, size_t count);
	int n_write = write( fd,buf,strlen(buf));
	if(n_write != -1)
	{
		printf("write %d byte to file1 \n", n_write);
	}



//	close(fd);
//	fd = open("./file1",O_RDWR);



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



	char *readBuf;
	readBuf = (char *)malloc(sizeof(char)*n_write+1);
	//ssize_t read(int fd, void *buf, size_t count);

	lseek(fd, 0, SEEK_SET);

	int n_read = read(fd, readBuf, n_write);
	printf("read %d ,contex:%s\n",n_read,readBuf);



	 close(fd);
 	 return 0;
}

在这里插入图片描述

注意:

可以将

lseek(fd, 0, SEEK_SET);//将光标移动到文件头

改写成

lseek(fd, -7, SEEK_CUR);//将光标从当前位置向前移动7字节(因为字符串长度就是7字节)

其中-7代表向前移动7字节;

7代表向后移动7字节。

(3)通过lseek()读文件大小

刚才向file1写入了“yrx0203”7个字节

通过如下函数,可读取file1的大小

在这里插入图片描述

在这里插入图片描述

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

int main()
{
        int fd;

        fd = open("./file1",O_RDWR);

        int File_size = lseek(fd,0,SEEK_END);
        printf("file_size  %d",File_size);
        close(fd);
        return 0;

}
~      



版权声明:本文为yrx020307原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。