无缓冲(Linux缓冲区的区别[亲测有效])

发布时间:2025-12-10 19:33:49 浏览次数:2

Linux缓冲区的区别[亲测有效]-

Linux缓冲区(无缓冲,行缓冲,全缓冲)的区别[亲测有效]Linux缓冲区(无缓冲,行缓冲,全缓冲)的区别1、缓冲区的概念1.1全缓冲区1.2、行缓冲1.3无缓冲1.4、控制缓冲区的方式2、demo3、结果分析1、缓冲区的概念缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/O)的数据作临时存储,这部分预留的内存空间就叫做缓冲区。1.1全缓冲区在这种情况下,在填满I/O缓冲区后再进行实际的I/O操作。对于驻留在磁盘上的文件通常由标准I/O库实施全缓冲。调用fflush函数冲洗一个流。冲洗意味着将缓冲区的内容写到磁盘上。对

Linux缓冲区(无缓冲,行缓冲,全缓冲)的区别

1、缓冲区的概念 1.1 全缓冲区 1.2、行缓冲 1.3 无缓冲 1.4、控制缓冲区的方式 2、demo 3、结果分析

1、缓冲区的概念

缓冲区(Buffer)就是在内存中预留指定大小的存储空间用来对输入/输出(I/O)的数据作临时存储,这部分预留的内存空间就叫做缓冲区。

1.1 全缓冲区

在这种情况下,在填满I/O缓冲区后再进行实际的I/O操作。对于驻留在磁盘上的文件通常由标准I/O库实施全缓冲。调用fflush函数冲洗一个流。冲洗意味着将缓冲区的内容写到磁盘上。

对应一个BUFFSIZE,只要没超过buffsize就能一直缓冲,一般在使用之前进行fflush,进行缓冲区的刷新,也就是清空缓冲区,但这里的清空的意思不是删除,只是把缓冲区的内容给逼出去。全缓冲一般是默认开启的。

1.2、行缓冲

在这种情况下,当在输入和输出遇到换行符时,标准I/O执行I/O操作。允许我们一次输出一个字符。涉及一个终端时,通常使用行缓冲。

类似”scanf()” “gets()” “puts()”
再遇到 \n 的时候进行输入或输出

1.3 无缓冲

标准I/O不对字符进行缓冲处理。例如:如果标准I/O函数fputs写15个字符到不带缓冲的流上,就会调用write的相关的函数立即写入打开的文件上。
read
write
strerr

1.4、控制缓冲区的方式

void setbuf(FILE *stream, char *buf) //buf参数必须指向一个长度为BUFSIZE的缓冲区int setvbuf(FILE *stream, char *buffer, int mode, size_t size)

是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活CLion DataGrip DataSpell dotCover dotMemory dotTrace GoLand PhpStorm PyCharm ReSharper ReShaC++ Rider RubyMine WebStorm 全家桶 刷新

【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…

参数 含义
stream 这是指向 FILE 对象的指针,该 FILE 对象标识了一个打开的流。
buffer 这是分配给用户的缓冲。如果设置为 NULL,该函数会自动分配一个指定大小的缓冲。
mode 这指定了文件缓冲的模式

模式的类型如下:

模式 含义
_IOFBF 全缓冲:对于输出,数据在缓冲填满时被一次性写入。对于输入,缓冲会在请求输入且缓冲为空时被填充。
_IOLBF 行缓冲:对于输出,数据在遇到换行符或者在缓冲填满时被写入,具体视情况而定。对于输入,缓冲会在请求输入且缓冲为空时被填充,直到遇到下一个换行符。
_IONBF 无缓冲:不使用缓冲。每个 I/O 操作都被即时写入。buffer 和 size 参数被忽略。

size –这是缓冲的大小,以字节为单位

2、demo

/* Example show usage of setbuf() &setvbuf() */#include<stdio.h>#include<error.h>#include<string.h>int main( int argc , char ** argv ){ int i;FILE * fp;char msg1[]="hello,wolrd\n";char msg2[] = "hello\nworld";char buf[128];//open a file and set nobuf(used setbuf).and write string to it,check it before close of flush the streamif(( fp = fopen("no_buf1.txt","w")) == NULL){ perror("file open failure!");return(-1);}setbuf(fp,NULL);memset(buf,'\0',128);fwrite( msg1 , 7 , 1 , fp );printf("test setbuf(no buf)!check no_buf1.txt\n");printf("now buf data is :buf=%s\n",buf);printf("press enter to continue!\n");getchar();fclose(fp);//open a file and set nobuf(used setvbuf).and write string to it,check it before close of flush the streamif(( fp = fopen("no_buf2.txt","w")) == NULL){ perror("file open failure!");return(-1);}setvbuf( fp , NULL, _IONBF , 0 );memset(buf,'\0',128);fwrite( msg1 , 7 , 1 , fp );printf("test setvbuf(no buf)!check no_buf2.txt\n");printf("now buf data is :buf=%s\n",buf);printf("press enter to continue!\n");getchar();fclose(fp);//open a file and set line buf(used setvbuf).and write string(include '\n') to it,////check it before close of flush the streamif(( fp = fopen("l_buf.txt","w")) == NULL){ perror("file open failure!");return(-1);}setvbuf( fp , buf , _IOLBF , sizeof(buf) );memset(buf,'\0',128);fwrite( msg2 , sizeof(msg2) , 1 , fp );printf("test setvbuf(line buf)!check l_buf.txt, because line buf ,only data before enter send to file\n");printf("now buf data is :buf=%s\n",buf);printf("press enter to continue!\n");getchar();fclose(fp);//open a file and set full buf(used setvbuf).and write string to it for 20th time (it is large than the buf)//check it before close of flush the streamif(( fp = fopen("f_buf.txt","w")) == NULL){ perror("file open failure!");return(-1);}setvbuf( fp , buf , _IOFBF , sizeof(buf) );memset(buf,'\0',128);fwrite( msg2 , sizeof(msg2) , 1 , fp );printf("test setbuf(full buf)!check f_buf.txt\n");printf("now buf data is :buf=%s\n",buf);printf("press enter to continue!\n");getchar();fclose(fp);}

3、结果分析

①、第一次是无缓冲,写进去七个字节成功。


②、第二次无缓冲,依旧写进去了


③、第三次行缓冲,应为字符串中带有“\n”,所以只写进来了\n之前的内容。但后续的字符串并且在缓冲区内


④、我们进行全缓冲的时候,发现什么都没写进去。但是字符串信心保存在缓冲区内。

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477