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;
}
}
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字符數組已經釋放,導致輸出詭異亂碼。
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來動態申請內存。