Thread: Determining whether stdin is redirected via command line..

  1. #1
    Deepcore Programmer
    Join Date
    Sep 2004
    Posts
    4

    Determining whether stdin is redirected via command line..

    Hi all - I'm new to this forum, though been around c programming for quite some while, now and then, again and again. Hope to enjoy your advices and give out some of mine, if I can.

    Here's the problem: I'm writing a small piece of software, that uses the Huffman character encoding algorithm, to compress files. Everything else's worked out and the program itself is working, but - I'd need the program to do something like this. When the following is given in the command line:

    >compress.exe < filename.txt > filename.cmp

    where compress.exe is my program, filename.txt is the source file and filename.cmp is the destination file. The program should compresses filename.txt and write compressed data to filename.cmp. I know how to handle this:

    Code:
    int main(int argc, char *argv[]) 
    {
        FILE *infile = stdin;
        FILE *outfile = stdout;
    
        compress(infile,outfile);
    
        ...
    
        return 0;   
    }
    but how can I check in the beginning, if the standard I/O streams HAVE indeed been redirected from and to a file. Because without this check, given for example, a following command:

    >compress.exe

    The program begins to wait for user keyboard input, to get data into the stdin stream. How could I avoid this, making sure that the stdin comes from an actual file instead of the keyboard ?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Last edited by Dave_Sinkula; 10-01-2004 at 01:25 PM. Reason: Noticed '.exe' and found a Windows link, which had an example for stdout.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I don't think it's possible. You could however force them to input the source filename. Something like this:
    Code:
    int main(int argc, char **argv)
    {
      FILE *infile;
    
      if(argc >= 2)
        infile = fopen(argv[1], "r");
      else
        infile = stdin;
    
      // rest of program follows
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Deepcore Programmer
    Join Date
    Sep 2004
    Posts
    4
    Thanks, Dave_Sinkula. The _isatty() actually does work, very nicely even - at least in win32 and SunOS, probably on linux as well. Whew... library developers really have thought of everything, haven't they...

    itsme86The idea's good and I considered that, too - but it still leaves us in the spot where, if no parameters are given and no input/output is redirected, the program starts waiting for user input, if it tries to read from stdin. Dave's answer might be a universal solution to the problem.

    Thanks again for the great help! ( I ought to remember now to bookmark some of the MSDN pages.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. Determining if stdin stream holds data
    By Stack Overflow in forum C Programming
    Replies: 3
    Last Post: 01-28-2005, 02:52 PM
  4. Question About Reading Lines from stdin
    By Zildjian in forum C Programming
    Replies: 7
    Last Post: 11-12-2003, 05:45 AM
  5. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM