如何知道 std::cin 是否来自重定向?

Roe*_*rel 5 c++ windows command-line std cin

假设我有一个小程序,例如:

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..."
} 
Run Code Online (Sandbox Code Playgroud)

仅当输入来自控制台时,我才想打印对参数的请求,但如果程序是通过重定向启动的,myApp.exe < textfile.txt那么我认为打印它没有意义。

我怎样才能实现这种行为?

编辑-我正在 Windows 上工作。

Roe*_*rel 3

这个答案基于@JoachimPileborg 评论

#include <io.h>
#include <stdio.h>
bool isInputRedirected()
{
    return (!(_isatty(_fileno(stdin))))
}
Run Code Online (Sandbox Code Playgroud)

原始出处MSDN