Thread: reading from stdin

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    reading from stdin

    hello

    I can't figure out how to make a program which won't get paused reading from the stdin.
    Here is an example:

    Code:
    string input_line;
    
    while (true) {
    
       if ("stdin is not empty") {
          cin >> input_line;
       }
    
       ... (some code to execute continuously)
    }
    I'm going to execute the program under linux so:

    Code:
    $ tail -f file.txt | ./program
    thanks in advance for help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if ("stdin is not empty") {
    There's really no portable way to do this without blocking for input if the stream really is empty. But if you search for kbhit, you can probably find some suitable implementation-dependent solutions.
    My best code is written with the delete key.

  3. #3
    coder
    Join Date
    Feb 2008
    Posts
    127
    Hi Prelude

    I've looked about kbhit()
    That function (wich seems being working only under borland-family compilers) concerns the key management, that is not what I'm searching for.
    A web research sends me around keyboard-input related functions, which I already know.

    My program needs to do his own stuff until it will check that the stdin is not empty.

    Thanks for the answer anyway

    edit: I don't need it to be portable, at the moment, I'm working under linux

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Then I don't understand your problem. Can you describe it in a different way?
    My best code is written with the delete key.

  5. #5
    coder
    Join Date
    Feb 2008
    Posts
    127
    ok

    Imagine a server software that manages, say, a chat network,
    running in background.
    If I want to send some messages to the server (like restart or shutdown, etc.)
    I should do that through a temporary file, which the program will read everytime I add a new line to it.

    Using for example "cin.get ()" will pause the server, until I send it a new message.

    So I'd need something like "cin.empty ()" to check if the server is receiving any message from the stdin or not.

    Hope it is clearer now.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I see. Well it's not portable, so I can't guarantee that it'll work for you, but try in_avail:
    Code:
    if ( cin.rdbuf()->in_avail() > 0 ) {
      cin >> input_line;
    }
    My best code is written with the delete key.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What about cin.peek()? I'm not sure if peek() blocks or just returns EOF if nothing is in the buffer?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What about cin.peek()?
    peek is built around sgetc, which calls underflow if it can't read anything, which isn't guaranteed not to block.
    My best code is written with the delete key.

  9. #9
    coder
    Join Date
    Feb 2008
    Posts
    127
    in_avail should work, as c++ reference says:
    http://www.cplusplus.com/reference/i.../in_avail.html

    but my test program didn't work...
    any suggestion about?

    edit: looking for peek ()

    Code:
    // test.cpp
    int main ()
    {
    	string line;
    
    	cout << "Input: ";
    	cin >> line;
    
    	cout << "in_avail: " << cin.rdbuf ()->in_avail () << endl;
    
    	return 0;
    }
    shell output:

    Code:
    $ g++ -o test test.cpp
    $ ./test
    Input: hello
    in_avail: 0

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    On linux you could use select().
    You probably have to set stdin to unbuffered mode.

  11. #11
    coder
    Join Date
    Feb 2008
    Posts
    127
    checked out: peek () blocks the program.

    You probably have to set stdin to unbuffered mode.
    what does it mean?

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by carlorfeo View Post
    in_avail should work, as c++ reference says:
    http://www.cplusplus.com/reference/i.../in_avail.html
    It says that in_avail calls showmanyc(). That function can check if underflow is possible, and return the number of available characters. However there is no requirement that cin implements this functionality; the default behavior is simply to return 0.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should be able to do this using "select()", perhaps something like this:
    Code:
    bool checkifdata(FILE *f)
    {
       int fno = fileno(f);
       fd_set fdset;
       FD_ZERO(fdset);
       FD_SET(fno, fdset);
       
       return select(fno+1, fdset, NULL, NULL, NULL);
    }
    Sorry if this is a late reply, but I got sidetracked.

    --
    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.

  14. #14
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by King Mir
    It says that in_avail calls showmanyc(). That function can check if underflow is possible, and return the number of available characters. However there is no requirement that cin implements this functionality; the default behavior is simply to return 0.
    Ok thanks, I don't know what "underflow" means exactly, but I figured out what the matter is.

    -----------------------------
    select() seems to be the right way, checking it out...

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by carlorfeo View Post
    what does [unbuffered] mean?
    matsp and other linux people can explain that. I haven't worked on unix for quite a few years. And I'm just guessing that stdin would have to be unbuffered for what you want. That may not be the case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  2. stdin + (ctrl+z), how i detect it?
    By Olimpo in forum C Programming
    Replies: 1
    Last Post: 09-30-2006, 05:33 AM
  3. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM
  4. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM