【发布时间】:2013-12-25 11:08:22
【问题描述】:
假设我有一个像这样的小程序:
int main()
{
cout << "Please enter the param >" << endl; // <-- Print only if input from console
std::string param;
cin >> param;
//Doing some calculations...
cout << "result is..."
}
我只想在输入来自控制台时打印参数请求,但如果程序以重定向 myApp.exe < textfile.txt 启动,那么我认为打印它没有意义。
我怎样才能实现这种行为?
编辑 - 我在 Windows 上工作。
【问题讨论】:
-
我猜如果你使用让我们说
scanf(),你不会有这个问题,或者你会吗? -
由于您使用的是 Windows,因此您可能可以使用
_isatty。 -
@khajvah - 不,
scanf()将导致相同的行为。 ): -
@JoachimPileborg - 如果你把它写成答案,我会接受它(:
-
我无法评论 Roee Gavirel's answer 因为 _isatty 有一些特殊性,如所述,例如,here。因此,最好使用GetConsoleMode 来代替它。特别是,GetConsoleMode 用于检测 Microsoft C 运行时库内部的控制台。
标签: c++ windows command-line std cin