c语言情绪缓存

1.用setbuf(stdin, NULL);清空缓存
setbuf的函数原型是
void setbuf(FILE *stream ,char *buf);
 
2.char c;
char c;
 BiTree *b;
 printf("\t1.create a bitree\n\t2.preorder\n\t3.middle order\n\t4.post order\n\t5.count the tree's node\n\t6.count the tree's hight\n\t0.exit\n");
 printf("enter your select :\n");
 while(scanf("%d",&select))
 {
  switch(select)
  {
   case 1://fflush();
   scanf("%c",&c);//按回车键的时候回车键也被存进去 //需要清楚缓存
   
   b=Bi_create();
   printf("create successfull!\n");
   break;
       }
}
fflush  不是一个表准的库函数  只有在vc中可以被识别  在gcc中不被识别
setbuf(stdin, NULL)刷新输入缓冲区
setbuf的一个误区
int main()
{
    int c;
    char buf[BUFSIZ];
    setbuf(stdout,buf);
    while((c = getchar()) != EOF)
        putchar(c);
   
    return 0;
}问题是这样的:程序交回控制给操作系统之前C运行库必须进行清理工作,其中一部分是刷新输出缓冲,但是此时main函数已经运行完毕,buf缓冲区作用域在main函数中,此时buf字符数组已经释放,导致输出诡异乱码。
解决方案:可以将buf设置为static,或者全局变量,或者调用malloc来动态申请内存。

你可能感兴趣的:(C语言,清除缓存)