首页 > 编程语言 > 详细

[C]C语言的缓冲

时间:2020-02-02 14:13:53      阅读:70      评论:0      收藏:0      [点我收藏+]

Note

  1. Standard IO are bufferd
  2. One is full buffering,this is,it will not output to the specified file until the buffer is full
  3. Another buffer is line buffer,which is not cleared until a newline is encountered.How to define end of file? C use EOF to define the end of file.So why use EOF to define the end of file ,because the decimal number corresponding to character is not -1,and EOF is defined as -1.

技术分享图片

// Program echo
// What you type,what the program returns to you.
#include <stdio.h>

int main()
{
    int ch;
    int start=1;// Used to mask whether to output leading character
    while ((ch=getchar())!=EOF) {
        if (start) {//if true then output leading character.
            putchar('>');
        }
        //Because keyboard input is line-buffered,it is checked whether
        //character ,the leading flag is set to true and a newline is
        //the character is a newline character when reading .If it is a newline output.
        if (ch=='\n') {
            start = 1;
            putchar(ch);
            continue;
        }
        // if the character is not newline,then output and set leading flag as false.
        putchar(ch);
        start=0;
        
    }
    // getchar encounter EOF when you type ctrl+d
    puts("end of echo");
    return 0;
}

Result:
技术分享图片

[C]C语言的缓冲

原文:https://www.cnblogs.com/tailiang/p/12251794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!