cngkaygusuz
cngkaygusuz

Reputation: 1506

Is there are a way to find out whether the input is streaming into the program from a file or not?

In Windows, there exists a console trick

someprogram.exe < input.txt

which makes the program to get the input from input.txt whenever there is a input request. I want my program to behave differently when the input is read from another file. Is there are a way to do that? How?

Upvotes: 1

Views: 105

Answers (2)

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

I don't think so(not sure though), but here is an alternative (error checking omitted):

int main(int argc, char **argv)
{
    std::istream * pstream = &std::cin;

    std::ifstream fin;
    if (argc > 1)
    {
        fin.open(argv[1]);
        pstream = &fin;
    }

    // use pstream instead of cin
}

Then you pass the name of the file as a command line argument.

Upvotes: 1

edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31861

Yes, use the function isatty available on most platforms. Looks like it is now called _isatty in windows (not sure why).

Upvotes: 0

Related Questions