linux文件缓冲区

背景

之前在linux下手写c语言日志库的时候,遇到日志内容无法及时刷新的情况,百思不得其解;最后查资料才知道是默认的缓冲区设置的问题。‘

与之相关的缓冲区设置函数有setbuf和setvbuf

分析

缓冲区分类:1.行缓冲 2.全缓冲 3.无缓冲

行缓冲特点是:

  • 默认大小:1024字节;
  • 遇换行符或缓冲区满时,将刷新缓冲区;

比如,标准输入输出就是行缓冲类型。

全缓冲特点是:

  • 默认大小:4096字节;
  • 缓冲区满时,将刷新缓冲区;

比如,普通文件读写缓冲区就是全缓冲类型。

无缓冲特点是直接调用系统IO刷新缓冲区,比如标准错误的缓冲区

磁盘文件缓冲区

通过setbuf和setvbuf函数,可以对文件缓冲区类型和大小进行修改。

 

测试代码

#include <stdio.h>                                                                                                                                                                                                       
#include <stdlib.h>
int main()
{
    int i;
    FILE *fp;
    char msg[] = "hello,world\n";
    char buf[128];
    if((fp = fopen("setbuf_nobuff.txt","w")) == NULL)
    {
        perror("file open fail");
        exit(-1);
    }
    setbuf(fp,NULL); // NO_BUFF
    fwrite(msg,7,1,fp);
    printf("test setbuf(NULL)\n");
    printf("press enter to continue\n");
    getchar();
    fclose(fp);
 
    if((fp = fopen("setvbuf_ionbf.txt","w")) == NULL)
    {
        perror("setvbuf_ionbf.txt");
        exit(-1);
    }
    setvbuf(fp,NULL,_IONBF,0);
    fwrite(msg,7,1,fp);
    printf("test setvbuf(_IONBF)\n");
    printf("press enter to continue\n");
    getchar();
    fclose(fp);
    if((fp = fopen("setvbuf_iolbf.txt","w")) == NULL)
    {
        perror("setvbuf_iolbf.txt");
        exit(-1);
    }
    setvbuf(fp,buf,_IOLBF,sizeof(buf));
    fwrite(msg,sizeof(msg),1,fp);
    printf("test setvbuf(_IOLBF)\n");
    printf("press enter to continue\n");
    getchar();
    fclose(fp);
 
    if((fp = fopen("setvbuf_iofbf.txt","w")) == NULL)
    {
        perror("setvbuf_iofbf.txt");
        exit(-1);
    }
    setvbuf(fp,buf,_IOFBF,sizeof(buf));
    for(i = 0;i < 2;i++)
    {
        fprintf(fp, msg);
    }
    printf("test setbuf(_IOFBF)\n");
    printf("press enter to continue\n");
    getchar();
    fclose(fp);
    return 0;
}

posted on 2019-12-29 13:47  sermatec江  阅读(979)  评论(0编辑  收藏  举报