【发布时间】:2014-05-17 15:53:54
【问题描述】:
我是缓冲流的新手。我正在编写一个简单的c程序,它将一个字符串作为用户输入并显示回来。我的工作环境是windows下的eclipse。代码如下:
#include<stdio.h>
enum { max_string = 127 };
static char string[max_string+1] = "";
void main(){
printf("type the input string---> \n");
fgets(string,max_string,stdin);
printf("the input string was ---> %s\n",string);
}
在运行时,首先获取用户输入,然后执行两个printf()'s。
输出样本为:
user input
type the input string--->
the input string was ---> user input
我在CodeBlocks IDE中尝试了上面的代码,它运行良好。输出如下:
type the input string--->
user input
the input string was ---> user input
有什么问题?
我还在printf() 的最后添加了一个\n,以便立即刷新它们。
问候。
【问题讨论】:
-
这似乎是一个 Eclipse/Windows 问题。第一个输出块绝对不是您对行为良好的运行时环境所期望的。
-
@RSahu,那我应该怎么做才能让它工作。
-
在
printf之后尝试fflush(stdout); -
@Prakash,谢谢。我想知道,是否有任何永久解决方案可以做到这一点。我的意思是,有什么办法可以禁用这个 extra 缓冲。
-
使用哪个版本的 Eclipse 您正在观察这种行为。还有它在哪个操作系统上运行。我无法在 Debian Stable 上的 Eclipse Juno SR1 上重现它。