IO学习系列之缓冲区

  • 缓冲区(本文特指标准IO的缓冲区):
  • 缓冲区的类型:
  • 一、行缓冲:
  • 终端相关的缓冲区;
  • 例如:标准输入(stdin)、标准输出(stdout);
  • 内存空间的大小:
  • 1KB1024B);
  • 二、全缓冲:
  • 文件相关的缓冲区;
  • 内存空间的大小:
  • 4KB4096B);
  • 三、无缓冲:
  • 没有缓冲区;
  • 例如:标准出错(stderr);
  • 内存空间的大小:
  • 0KB
  • 验证三种类型缓冲区的大小:
  • 代码如下
	#include 
	#include 
	#include 
	
	int main(int argc, char const *argv[])
	{
	    fgetc(stdin);
	
	    printf("stdin buf size = %ld\n",stdin->_IO_buf_end - stdin->_IO_buf_base);
	
	    printf("stdout buf size = %ld\n",stdout->_IO_buf_end - stdout->_IO_buf_base);
	    
	    fputc('F',stderr);
	    puts("");
	    printf("stderr buf size = %ld\n",stderr->_IO_buf_end - stderr->_IO_buf_base);
	    
	    FILE *fp;
	    if(NULL == (fp = fopen("./test.txt","w")))
	    {
	        perror("fopen error");
	    }
	    fputc('T',fp);
	    printf("fp buf size = %ld\n",fp->_IO_buf_end - fp->_IO_buf_base);
	    fclose(fp);
	    return 0;
	}



  • 运行结果:
	k
	stdin buf size = 1024
	stdout buf size = 1024
	F
	stderr buf size = 1
	fp buf size = 4096
  • 注意:
  • stderr buf size = 1,这是因为向标准出错中写入了一个字符‘F’,所以无缓冲区,即内存空间大小为0KB
  • 行缓冲刷新方式:
  • 1、遇到换行符时,会刷新缓冲区;
  • 2、程序结束时,会刷新缓冲区;
  • 3、关闭文件指针时,会刷新缓冲区;
  • 4、输入输出切换时,会刷新缓冲区;
  • 5、行缓冲区达到1KB时,会刷新缓冲区;
  • 6、调用fflush函数时,会刷新缓冲区(即手动刷新缓冲区);
  • 代码如下
	#include 
	#include 
	#include 
	
	int main(int argc, char const *argv[])
	{
	    // // - 1、遇到`换行符`时,会刷新缓冲区;
	    // printf("I Love China!!!\n");
	
	    // // - 2、`程序结束`时,会刷新缓冲区;
	    // printf("I Love China!!!");
	
	    // // - 3、关闭`文件指针`时,会刷新缓冲区;
	    // printf("I Love Beijing");
	    // fclose(stdout);
	    
	
	    // - 4、`输入输出切换`时,会刷新缓冲区;
	    int a = 0;
	    printf("请输入一个数:>>");
	    scanf("%d",&a);
	    
	
	    // - 5、行缓冲区达到`1KB`时,会刷新缓冲区;
	
	    for(int i = 0; i < 1025; ++i)
	    {
	        fputc('k',stdout);
	    }
	    
	    // - 6、调用`fflush函数`时,会刷新缓冲区(即`手动刷新`缓冲区);
	    printf("I Love Beijing");
	    fflush(stdout);
	    
	
	    return 0;
	}
  • 运行结果:
请输入一个数:>>5
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkI Love Beijing
  • 全缓冲刷新方式:
  • 1、程序结束时,会刷新缓冲区;
  • 2、关闭文件指针时,会刷新缓冲区;
  • 3、输入输出切换时,会刷新缓冲区;
  • 4、全缓冲区达到4KB时,会刷新缓冲区;
  • 5、调用fflush函数时,会刷新缓冲区(即手动刷新缓冲区);
  • 注意:
  • 遇到换行符时,全缓冲不会刷新缓冲区
  • 代码如下
	#include 
	#include 
	#include 
	
	int main(int argc, char const *argv[])
	{   
	    FILE *fp = fopen("./test.txt","w");
	    if(NULL == fp)
	    {
	        perror("fopen error");
	        exit(-1);
	    }
	    // - 1、遇到`换行符`时,不会刷新缓冲区;
	    fputc('e',fp);
	    fputc('\n',fp);
	
	    // - 2、`程序结束`时,会刷新缓冲区;
	    fputs("I Love Beijing",fp);
	
	    // - 3、关闭`文件指针`时,会刷新缓冲区;
	    // fputs("I Love HNGC",fp);
	    // fclose(fp);
	
	    // - 4、`输入输出切换`时,会刷新缓冲区;
	    fputs("hello world",fp);
	    char buf[32] = {0};
	    fgets(buf,32,fp);
	    
	    
	    // // - 5、全缓冲区达到`4KB`时,会刷新缓冲区;
	    for(int i = 0; i < 4097; ++i)
	    {
	        fputc('k',fp);
	    }
	    
	    // - 6、调用`fflush函数`时,会刷新缓冲区(即`手动刷新`缓冲区);
	    fputs("Miss U",fp);
	    fflush(fp);
	
	    return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