" << endl; ...">

How to find out if std :: cin is reassigned?

Let's say I have a small program like:

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..."
} 

I want to print the request for the parameter only if the input is from the console, but if the program was launched with redirection myApp.exe < textfile.txt, then I see no reason to print it.

How can I achieve this behavior?

Edit - I am working on windows.

+4
source share
2 answers

This answer is based on @JoachimPileborg's comment.

#include <io.h>
#include <stdio.h>
bool isInputRedirected()
{
    return (!(_isatty(_fileno(stdin))))
}

Source Source on MSDN

+3
source

: ++ , AFAIK ( ) Boost.


, , - , - , , , , .

, / , .

+1

All Articles