Hi all:

Try to do some programming under Linux.
The programme I am writing is supposed to read all the char from a file via "command in line argument (i.e. agv)" and via redirection (i.e. read <FILE_NAME.

In my program, I have written:

FILE *stream;

stream = fopen(argv,"r");

if (stream==NULL) /*The file name is not in the command line argument*/


I am wondering what I should do now to check if there is a redirection stdin.


Many thanks

If you wish, your program can assume that if there was nothing on the command line, redirection is being used. In that case, instead of using the fopen(), simply assign stdin to your stream:

FILE *stream;

if (argc < 2)
{
    stream = stdin;
}
else
{
    stream = fopen(argv[i],"r");
    if (stream==NULL) 
   ...
}

This allows your program to take a file
> prog file.name

redirected file
>prog <file.name

or from the keyboard
>prog

Thanks man. Really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.