<分区>
Possible Duplicate:
Does reading from stdin flush stdout?
C++ 标准保证在下次调用 std::cin 之前打印缓冲区中包含的所有数据。像这样:
#include <iostream>
void bar()
{
int x;
std::cout << "Enter an integer: "; /* 1 */
std::cin >> x; /* 2 */
}
因为这个:
ISO/IEC 14882:2011
27.4.2 Narrow stream objects [narrow.stream.objects]
2 After the object cin is initialized, cin.tie() returns &cout. Its state is otherwise the same as required for basic_ios::init (27.5.5.2).
27.4.3 Wide stream objects [wide.stream.objects]
2 After the object wcin is initialized, wcin.tie() returns &wcout. Its state is otherwise the same as required for basic_ios::init (27.5.5.2).
但在 C 中,真的不能保证在任何尝试 stdin 之前都会打印 stdout 缓冲区中包含的所有内容吗?
#include <stdio.h>
void bar()
{
int x;
printf("Enter an integer: "); /* 1 */
scanf("%d", &x); /* 2 */
}
我知道 stdout 是行缓冲的,但我不想在这种情况下放置 '\n' 字符。使用 fflush/fclose/etc 是在 C 中输入请求之前获得输出的唯一正确方法吗?