首页 > 其他分享 >关于read指向缓冲区的理解

关于read指向缓冲区的理解

时间:2022-08-17 12:44:13浏览次数:60  
标签:指向 read int fd 缓冲区 include buf

read在linux原型定义如下:

 

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);

关于buf,man手册解释如下:

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

也就是说,read读取数据后,放到buf缓冲区中,从哪开始放呢?是从buf的开始位置开始放,什么是开始位置呢,这个位置是可以通过p指针进行指定的

我们看如下代码:

假设test.txt中保存了a-z个字符。

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <sys/stat.h>
 4 #include <fcntl.h>
 5 #include <errno.h>
 6 #include<unistd.h>
 7 #include<stdlib.h>
 8 #include<string.h>
 9 int main()
10 {
11     int fd=open("test.txt",O_RDONLY);
12     if (fd==-1){
13         perror("open");
14         exit(EXIT_FAILURE);
15     }   
16     char buf[1024]={"\0"};
17     char* p=buf;
18 
19     while(read(fd,p,1)!=0){
20         //printf("%d\n",*p);                                                                                                                                                                                                                                                  
21         //p++;
22     }   
23     printf("%s\n",buf);
24 
25 
26     close(fd);
27     return 0;
28 }

注意第20行,此时将这行注释掉了,那么read循环读入的时候,每次都存放到p指向的地址,而p没有变化,所以每次读取的字符都会覆盖掉上次写入的数据,最后结果为z。

但是如果将该行注释取消,那么每次read读取的字符就会放到p指针指向的位置,也就是说p加1的位置,这样就会把所有的a-z经过多次循环写入到缓冲区中,显示结果为“abcdefghigklmnopqrstuvwxyz”

 

 

标签:指向,read,int,fd,缓冲区,include,buf
From: https://www.cnblogs.com/simon-xie/p/16594716.html

相关文章

  • readme 用法相关详细文档
    https://docs.github.com/cn/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax基本撰写和......
  • 力扣|Q1834单线程CPU-SingleThreadedCPU
    Q1834SingleThreadedCPU简介给你一个二维数组tasks,用于表示n​​​​​​项从0到n-1编号的任务。其中tasks[i]=[enqueueTimei,processingTimei]意味着第i......
  • 9.NIO 核心1:缓冲区(Buffer)
    staticxxxBufferallocate(intcapacity):创建一个容量为capacity的xxxBuffer对象  样例:@Testpublicvoidtest01(){//1.分配一个缓冲......
  • 8、ThreadPoolTaskExecutor线程并发
    一、线程池的优点:1、降低资源消耗。通过重复利用自己创建的线程降低线程创建和销毁造成的消耗。2、提高响应速度。当任务到达时,任务可以不需要等到线程创建就能立即执行......
  • 改变this指向
    改变this指向#callapplybind:是所有函数都具有的方法函数也是对象,函数具有方法call(参数1,参数2,参数3.。。)参数1:调用函数时,内部this的具有值剩余参数(参数2,......
  • java.lang.IllegalStateException: getWriter() has already been called for this re
    异常出现原因是response在获取字节流之前会判断字符流是否已使用,反之获取字符流之前也会判断字节流是否已使用,若判断已使用则抛出异常getWriter()/getOutStream()hasalre......
  • 从Thread.start入手Hotspot源码
    nativestart0追踪到hotspot源码中privatevoidnativestart0();native的原理是调用JNI,而Hotspot源码的惯例则是,通常一个Xxx.java对应一个Xxx.c,以下举三个例子:......
  • Error when reading /var/lib/rabbitmq/.erlang.cookie: eacces
    在部署rabbitmq集群的时候,将A服务器上的.erlang.cookie复制到B服务器上的.erlang.cookie然后,在启动B服务的上rabbitmq时,报错Error:Failedtoinitializeerlangdis......
  • 关于ThreadLocal的使用
    定义上下文publicclassThreadContext<T>{privatestaticfinalThreadLocal<ThreadContext<?>>LOCAL=newThreadLocal<>();privateThreadContext(){}......
  • this关键字的指向
    this关键字的指向1.方法中的this,指向调用方法的对象2.全局环境下指向全局对象3.全局函数中的this,指向全局对象4.内部函数中的this,指向全局对象5.时间中的this,指向触发......