zoukankan      html  css  js  c++  java
  • stderr,stdout,a.txt缓冲区别

    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #include<error.h>
    #include<errno.h>
    #include<unistd.h>
    #include<strings.h>
    #include<stdbool.h>

    #include<sys/stat.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<fcntl.h>
    int main(int argc,char **argv)
    {
    fputs("to stdout",stdout);//行缓冲,只有字符串后面加油‘ ’,才会显示到屏幕
    fputs("to stderr",stderr);//无缓冲,直接显示到屏幕
    FILE *fp = fopen("a.txt","w");//全缓冲,一般为4k,windows为1k..
    if(fp == NULL)
    {
    fprintf(stderr,"fopen()%s failed:%s","a.txt",strerror(errno));
    exit(0);
    }
    fputs("to a.txt",fp);
    pause();

    fflush(fp);//强制刷新全缓冲。
    // return 0;
    }

    ----------------------------------------------

    几条刷新注意:

    关于标准IO缓冲区
    ================================
    1,不缓冲(标准出错流stderr)
    1.1 一旦缓冲区中有数据,立即刷新。

    2,全缓冲(普通文件)
    2.1 一旦把缓冲区填满,立即刷新(做练习:检测全缓冲的大小)
    2.2 调用fflush()强制刷新。
    2.3 程序正常退出时,立即刷新。
    2.3.1 在main函数中return
    2.3.2 在任意地方调用exit()/_exit()/_Exit()
    2.3.3 在最后一条线程中调用pthread_exit()
    2.4 调用fclose()关闭文件时,立即刷新。
    2.5 调用setvbuf()/setbuf()更改缓冲区类型时,立即刷新。

    3,行缓冲(标准输出流stdout)
    3.1 一旦把缓冲区填满,立即刷新(做练习:检测全缓冲的大小)
    3.2 调用fflush()强制刷新。
    3.3 程序正常退出时,立即刷新。
    3.3.1 在main函数中return
    3.3.2 在任意地方调用exit()/_exit()/_Exit()
    3.3.3 在最后一条线程中调用pthread_exit()
    3.4 调用fclose()关闭文件时,立即刷新。
    3.5 调用setvbuf()/setbuf()更改缓冲区类型时,立即刷新。
    3.6 一旦遇到' '

  • 相关阅读:
    LeetCode5 Longest Palindromic Substring
    LeetCode4 Median of Two Sorted Arrays
    LeetCode3 Longest Substring Without Repeating Characters
    LeetCode2 Add Two Numbers
    LeetCode1 Two Sum
    算法总结—深度优先搜索DFS
    c++ 设计模式9 (Abstract Factory 抽象工厂模式)
    题解 P3374 【【模板】树状数组 1】
    题解 HDU1565 【方格取数(1)】
    题解 P2431 【正妹吃月饼】
  • 原文地址:https://www.cnblogs.com/defen/p/5190278.html
Copyright © 2011-2022 走看看