目录

 1.read函数简介:

1.包含的头文件

 2.函数原型

3.函数参数说明:

4.write函数描述 

 5.函数返回值

 2.代码demo:

 1.为什么使用了read函数之后读取不到数据?(光标的问题)

2.用土方法解决光标问题读取数据,代码demo:


1.read函数简介:

1.包含的头文件

         #include <unistd.h>

 2.函数原型

 ssize_t read(int fd, void *buf, size_t count);

3.函数参数说明:

  • int fd :文件描述符
  • const void *buf :一个无类型的指针buf,是一个缓冲区
  • size_t count:你要读取文件的大小

【整个函数的意思是:从 fd 指向的文件里面的数据,读取多少个字节数据到缓存区 buf 里面去。】

4.write函数描述 

DESCRIPTION
       read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

       If count is zero, read() returns zero and has no other results.  If count is greater than SSIZE_MAX, the result is unspecified.

read()尝试从文件描述符fd读取字节数到从buf开始的缓冲区中。如果count为0,则read()返回0并且没有其他结果。如果count大于SSIZE_MAX,则结果是未指定的

 5.函数返回值

RETURN VALUE
       On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number.  It is not an error if this number is smaller than the number of bytes requested; this may happen for example because  fewer  bytes  are actually  available  right  now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a termi‐nal), or because read() was interrupted by a signal.  On error, -1 is returned, and errno is set appropriately.  In this case it  is left unspecified whether the file position (if any) changes.

如果成功,将返回读取的字节数(0表示文件结束),并且文件位置将以这个数字向前推进。如果这个数字小于请求的字节数,则不会报错;例如,这可能是因为现在实际可用的字节数更少(可能因为我们接近文件结束符,或者因为我们正在从管道读取数据)。或者由于read()被一个信号中断。出现错误时,返回-1,并适当设置ermo。如果文件的位置发生了变化,它是不指定的。

 2.代码demo:

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

int main()
{
        int fd;
        char *buf = "wenjian chu ru men !";

        fd = open("./file1",O_RDWR);        //打开文件

        if(fd == -1){
                printf("open file1 fail \n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);        //如果没有文件,创建文件
                if(fd > 0){
                        printf("creat file1 success \n");
               }
        }

        printf("open file1 success: fd = %d \n",fd);

        //write函数原型:ssize_t write(int fd, const void *buf, size_t count);
        int n_write =  write(fd,buf,strlen(buf));
        printf("write %d byte to file1 \n",n_write);


        char *readbuf;
        readbuf = (char *)malloc(strlen(buf)+1);
        //read函数原型:ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readbuf,n_write);
        printf("read %d byte,context:%s\n",n_read,readbuf);


        close(fd);
      
        return 0;
}
     

 1.为什么使用了read函数之后读取不到数据?(光标的问题)

因为:在我们write写操作之后,光标已经移动到了数据最末尾的位置,(就像你用word文档敲完一句话之后,那个光标总是停留在最后的位置),这里是同样的道理。所以当我们read读操作的时候,总是在末尾读数据,单数尾巴没有数据,所以啥也读不到。

有两种解决办法:

  1. 将光标移到头,在读取数据。
  2. 关闭文件,重新打开再读取。【这方法比较土,基本不用】

2.用土方法解决光标问题读取数据,代码demo:

关闭文件,重新打开再读取。【这方法比较土,基本不用】

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

int main()
{
        int fd;
        char *buf = "wenjian chu ru men !";

        fd = open("./file1",O_RDWR);            //打开文件

        if(fd == -1){
                printf("open file1 fail \n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);        //如果没有文件,创建文件
                if(fd > 0){
                        printf("creat file1 success \n");
               }
        }

        printf("open file1 success: fd = %d \n",fd);

        //write函数原型:ssize_t write(int fd, const void *buf, size_t count);
        int n_write =  write(fd,buf,strlen(buf));
        printf("write %d byte to file1 \n",n_write);


        close(fd);                        //关闭文件

        fd = open("./file1",O_RDWR);      //打开文件,这两步解决write写操作光标移动到末尾的问题(土方法,不常用)


        char *readbuf;
        readbuf = (char *)malloc(strlen(buf)+1);
        //read函数原型:ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readbuf,n_write);
        printf("read %d byte,context:%s\n",n_read,readbuf);


        close(fd);
      
        return 0;
}
     

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐