Hi all!
I have a code that asks the user to input some parameters from a command line using operator in a form
cin >> param;
Sometimes I pass these parameters as a whole file "mydata" from command line using
> ./myprog.out < mydata
Either way works fine for me. My questions is:
is there a way to check which of the two methods is currently executed?
Parameters such as argc and argv don't distinguish between the two, so there must be some other way ( if there really is... ).
Thanks for your answers!
Thanks.
I tried, but Google barely returns any constructive answer on isatty() referring to my case.
Could you maybe provide a very simple example?
My code may look like this:
1 2 3 4 5 6 7 8 9 10
#include<unistd.h>
// some other includes
int main(int argc, char * argv[] )
{
int a;
cin >> a;
/* blah-blah */
return 0;
}
If I create mydata as simply one-bite file
4
and pass it as
> ./myprog.out < mydata
how do I check in the code above that my variable was read from a file using isatty() ?
All I need is a simple condition check, nothing more. I do not even need the name of the file.
That's a good point, but that would look fine only if the data was read from a file. If it's read from the keyboard, the value of the variable will be printed twice. That's ugly.
On program startup, the integer file descriptors associated with the streams stdin, stdout, and stderr are 0, 1, and 2, respectively. The preprocessor symbols STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO are defined with these values in <unistd.h>.
(Applying freopen(3) to one of these streams can change the file descriptor number associated with the stream.)