Thread: Checking stdin for input

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    23

    Checking stdin for input

    I have an assignment where I have to redirect input from a file through stdin when the program starts.

    its executed like this:

    ./program < textfile

    The question is, I want to be able see if theres data in stdin when the program starts, otherwise when I try to process the data from stdin it just hangs until the user presses ctrl+d (on SunOS).

    Code I'm using:

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
       /* redirect input from the console to stdin */
       char *buf;
    
       while (fgets(buf, 1024, stdin)!=NULL)
       {
          printf("%s", buf);
       }
    
       free(buf);
       return 0;
    }

    So far I've tried to use feof() with no luck.

    Thanks in advance for any input.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no standard way to check for pending input. The standard doesn't care about particular hardware specifics for input, so it doesn't have something like kbdhit. You're going to need something OS/Compiler specific.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hawaiian robots View Post
    The question is, I want to be able see if theres data in stdin when the program starts, otherwise when I try to process the data from stdin it just hangs until the user presses ctrl+d (on SunOS).
    This question makes no logical sense. What you're asking is how to read minds. There is no way to tell if the user is going to enter any input. The only thing you can do is wait and see if they do. If they indicate their desire to stop entering input by pressing Ctrl+D, only at that time can you know if there is input.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could use isatty

    It requires a file-descriptor, and to make sure you are not relying on implementation of stdin, you should use fileno(stdin) to get the file-descriptor for stdin.

    Obviously, if stdin is the tty (keyboard input, essentially), then it isatty() will return non-zero. If it's a file, pipe or some such, then it will return zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    23
    Quote Originally Posted by brewbuck View Post
    This question makes no logical sense. What you're asking is how to read minds. There is no way to tell if the user is going to enter any input. The only thing you can do is wait and see if they do. If they indicate their desire to stop entering input by pressing Ctrl+D, only at that time can you know if there is input.
    Haha I know what you mean. TBH I don't like the fact that we are asked to redirect a file through stdin at the start of the program. The question I'm really asking is how to check to see if the user has indeed piped in the file on startup. The fact that the program hangs in of itself is an indicator, but not a very user friendly indicator.

    Quote Originally Posted by matsp
    You could use isatty
    It requires a file-descriptor, and to make sure you are not relying on implementation of stdin, you should use fileno(stdin) to get the file-descriptor for stdin.

    Obviously, if stdin is the tty (keyboard input, essentially), then it isatty() will return non-zero. If it's a file, pipe or some such, then it will return zero.
    I'll give that a try, thanks.

  6. #6
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by hawaiian robots View Post
    The question I'm really asking is how to check to see if the user has indeed piped in the file on startup. The fact that the program hangs in of itself is an indicator, but not a very user friendly indicator.
    Don't be user-friendly in console applications: users aren't friendly either. ;-)

    On a serious note, I suggest leaving it AS IS. There are a lot of tools that can take input from stdin (e.g. cat, sort, python), and if there is no input, they just wait. That's far better than guessing what an illiterate user wants to accomplish prior to having RTFM.

    Greets,
    Philip
    Last edited by Snafuist; 05-19-2009 at 06:35 AM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    another option might be to use select() to determine if stdin is ready for reading. then you can have a timeout and if there's no input, you can exit politely.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char *buf;
    
    while (fgets(buf, 1024, stdin)!=NULL)
    {
    You're not really trying to read into an uninitialized pointer are you? You free the pointer later on but I don't see a malloc anywhere.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM