Reading stdin

Discussion in 'C Programming' started by orium69@gmail.com, Jan 4, 2006.

  1. Guest

    Hi!
    how could i know if it was written something in stdin since the last
    time i read it?

    Tks...
     
    , Jan 4, 2006
    #1
    1. Advertising

  2. rayw Guest

    <> wrote in message
    news:...
    > Hi!
    > how could i know if it was written something in stdin since the last
    > time i read it?


    Not quite sure I've got this right, but you're asking [?] if I've read
    stdin - until it's empty, how can I then determine whether something else is
    waiting there to be read?

    Well, I guess you'd try to read something from stdin
     
    rayw, Jan 4, 2006
    #2
    1. Advertising

  3. Guest

    , Jan 4, 2006
    #3
  4. Guest

    rayw wrote:
    > Not quite sure I've got this right, but you're asking [?] if I've read
    > stdin - until it's empty, how can I then determine whether something else is
    > waiting there to be read?
    >
    > Well, I guess you'd try to read something from stdin


    My problem is that I need to read from stdin only if is something to
    read, i don want to break the program until somebody type something...
     
    , Jan 4, 2006
    #4
  5. pemo Guest

    <> wrote in message
    news:...
    > rayw wrote:
    >> Not quite sure I've got this right, but you're asking [?] if I've read
    >> stdin - until it's empty, how can I then determine whether something else
    >> is
    >> waiting there to be read?
    >>
    >> Well, I guess you'd try to read something from stdin

    >
    > My problem is that I need to read from stdin only if is something to
    > read, i don want to break the program until somebody type something...


    Sadly, you cannot monitor stdin [sadly] using a standard function without
    *perhaps* causing a 'wait'.

    What's you're OS - as they'll most likely be someway to this under that???
     
    pemo, Jan 4, 2006
    #5
  6. Flash Gordon Guest

    rayw wrote:
    > <> wrote in message
    > news:...
    >> Hi!
    >> how could i know if it was written something in stdin since the last
    >> time i read it?

    >
    > Not quite sure I've got this right, but you're asking [?] if I've read
    > stdin - until it's empty, how can I then determine whether something else is
    > waiting there to be read?
    >
    > Well, I guess you'd try to read something from stdin


    Which, unfortunately for the OP, does not tell you if anything more has
    been written to it (e.g. the user types something else, or more data in
    the pipe, or whatever) but instead makes your program wait until either
    end-of-file, an error condition, or some more data is entered.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 4, 2006
    #6
  7. a écrit :
    > My problem is that I need to read from stdin only if is something to
    > read, i don want to break the program until somebody type something...


    Can't do that in standard C. If you can, see select() or poll() (it's
    POSIX, hence very portable)

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 4, 2006
    #7
  8. writes:
    > how could i know if it was written something in stdin since the last
    > time i read it?


    The C FAQ is at <http://www.c-faq.com/>. You've asked question 19.2.

    (Don't people cite the FAQ anymore?)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 5, 2006
    #8
  9. On Wed, 4 Jan 2006 22:05:43 UTC, wrote:

    > rayw wrote:
    > > Not quite sure I've got this right, but you're asking [?] if I've read
    > > stdin - until it's empty, how can I then determine whether something else is
    > > waiting there to be read?
    > >
    > > Well, I guess you'd try to read something from stdin

    >
    > My problem is that I need to read from stdin only if is something to
    > read, i don want to break the program until somebody type something...
    >

    Impossible mission. There is nothing in standard C that defines that
    stdin is a device that will do what you thinks. stdin may return data
    until EOF is reached and nothing else thereafter or it may block the
    program until some new data arrives from anywhere it is bounded to or
    it may behave something else depending on the API you uses to read
    from stdin. It may even change its behavior from run to run depending
    on the device or file stdin is bounded to.

    C knows nothing about keyboards, punch cards, files, or other devices.
    stdin is simply an abstract stream that will serve a stream of
    characters until EOF is reached.

    When you really needs a specific behavior check the documentation of
    the system your program is running under and look if (or if not) that
    specific system gives you an API that does what you asks for.



    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    eComStation 1.2 Deutsch ist da!
     
    Herbert Rosenau, Jan 6, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,359
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    815
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,383
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    445
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    311
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page