Data input from file at command line

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!
Check out isatty()
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.
Last edited on
Why not have your app print out what was read from 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.
Google barely returns any constructive answer on isatty()

http://pubs.opengroup.org/onlinepubs/007908799/xsh/isatty.html
@kbw,
by the words that Google gives me no clue I mean exactly what you posted. I have no idea how parameter of isatty()

int fildes

is related to my istream object cin.
How can I connect the two?
Last edited on
man stdin wrote:
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.)

Ok, I will include

isatty(STDIN_FILENO)

in my code and see how it goes. I'll update the discussion thread tomorrow. Thanks to all who replied.
This worked like magic. A simple piece of code
1
2
3
4
5
6
int a;
if(isatty(STDIN_FILENO)) // interaction with user
{
  /* my stuff */
}
cin >> a;

does all the work. Thanks to all !!!
Topic archived. No new replies allowed.