Reputation: 1506
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
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
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