+ -
当前位置:首页 → 问答吧 → 【请教】加与不加else printf,导致终端“输出”或是出“现闪烁符等待”的疑惑

【请教】加与不加else printf,导致终端“输出”或是出“现闪烁符等待”的疑惑

时间:2010-08-11

来源:互联网

  1. for (dest_port = start_port; dest_port <= end_port; dest_port++) {
  2.                 if (sub_func(xxx,xxx,xxxx.....) == 1) {
  3.                         printf("\nport: %hu is open\n", dest_port);
  4.                 }
  5.                 else {
  6.                         //;                  
  7.                         //continue;
  8.                         //printf("port: %hu is closed\n", dest_port);
  9.                         //printf(".");
  10.                 }
  11.         }
复制代码
当else为 :

   printf("port: %hu is closed\n", dest_port);  时,可以正常执行并程序结束

但是,当没加else,或是加了 else,用的是  ; 或  continue或 printf(".";时, 当 dest_port从1记到1000时, 终端就不输出东西了,然后出现了等待的闪烁符,然后程序也不会结束。

不知道这是什么原因导致,感觉好奇怪,难道要加 fllush什么的吗?


以前没遇到过这种问题,还请大家帮忙分析下,谢谢!

作者: nine8   发布时间: 2010-08-11

补充下现象:


原来加了 else printf(xxxxxxxxxxxxxxxxx);的可以正常输出并结束的,

在终端执行程序+ option后面跟了 | tee > result.log

即  %> test -a xxx -b xxxx | tee > result.log

时也会出现光标闪烁符,一直等待


好奇怪,难道是终端的问题?

作者: nine8   发布时间: 2010-08-11

标准输出行缓冲的缘故, printf("port: %hu is closed\n", dest_port);  最后有\n,就输出了
你输出到 stderr应该就没这问题了

作者: hellioncu   发布时间: 2010-08-11

一个行缓冲难倒了无数英雄汉......

作者: liwangli1983   发布时间: 2010-08-11

“\n"试试

作者: snriyt   发布时间: 2010-08-11