stdin and stdout

Discussion in 'C Programming' started by Andrew, Oct 28, 2003.

  1. Andrew

    Andrew Guest

    Hello,

    I have read about stdin and stdout being standard input and output
    streams, but I am still having trouble visualizing how they really
    work and what they contain during program execution. For instance
    could you use a call to fputc() and/or fseek to write a carriage
    return to stdin and cause a subsequent call to getchar() to return
    without the user pressing a return key? I have checked the C FAQ and
    GNU C Library section on standard streams for more info and found a
    lot on their usage, but not much on the internals of how they are
    structured and operate. Thanks for any advice.
     
    Andrew, Oct 28, 2003
    #1
    1. Advertising

  2. "Andrew" <> wrote:
    > I have read about stdin and stdout being standard input and output
    > streams, but I am still having trouble visualizing how they really
    > work and what they contain during program execution. For instance
    > could you use a call to fputc() and/or fseek to write a carriage
    > return to stdin and cause a subsequent call to getchar() to return
    > without the user pressing a return key?


    No and no, in that order :) For two reasons. First, all std* streams
    are unseekable. Second, stdin is input only, trying to do anything
    other than read from it is a UB. (I believe that you can do ungetc
    on stdin, but I have been known to be wrong :))

    Similarily stdout and stderr are output only.

    > I have checked the C FAQ and
    > GNU C Library section on standard streams for more info and found a
    > lot on their usage, but not much on the internals of how they are
    > structured and operate. Thanks for any advice.


    Consider them ordinary files, except that you do not need to worry
    about opening and closing them, as that is done for you automagically.

    Peter
     
    Peter Pichler, Oct 28, 2003
    #2
    1. Advertising

  3. Andrew

    Eric Sosman Guest

    Andrew wrote:
    >
    > Hello,
    >
    > I have read about stdin and stdout being standard input and output
    > streams, but I am still having trouble visualizing how they really
    > work and what they contain during program execution. For instance
    > could you use a call to fputc() and/or fseek to write a carriage
    > return to stdin and cause a subsequent call to getchar() to return
    > without the user pressing a return key? I have checked the C FAQ and
    > GNU C Library section on standard streams for more info and found a
    > lot on their usage, but not much on the internals of how they are
    > structured and operate. Thanks for any advice.


    stdin is an input stream, hence output operations are
    not meaningful[*]. Dually, stdout and stderr are output
    streams, so input operations aren't usable. fseek() and
    other positioning functions may or may not work, and this
    may vary from one program execution to the next.

    The C Standard describes what an implementation may and
    must do, but not how it is to be done. P.J. Plauger's "The
    Standard C Library" considers many of the issues facing a C
    library implementor and exhibits a specimen implementation,
    but its internals may have little resemblance to those of
    the system(s) you're familiar with.

    [*] ungetc() is an "output-like" operation, in a very
    limited way. It will do what you're asking for,
    but not what I suspect you desire.

    --
     
    Eric Sosman, Oct 28, 2003
    #3
  4. (Andrew) wrote:

    >Hello,
    >
    >I have read about stdin and stdout being standard input and output
    >streams, but I am still having trouble visualizing how they really
    >work and what they contain during program execution.


    stdin, stdout and stderr are expressions of type 'pointer to FILE'
    defined in <stdio.h>. They designate streams opened in text mode,
    connected to the console by default on most (?) hosted
    implementations. They "contain" user supplied input and program
    generated output respectively.

    >For instance
    >could you use a call to fputc() and/or fseek to write a carriage
    >return to stdin and cause a subsequent call to getchar() to return
    >without the user pressing a return key?


    Not so. Write operations on stdin invoke undefined behaviour, as does
    fflush(stdin), just in case you're tempted to use this construct.

    >I have checked the C FAQ and
    >GNU C Library section on standard streams for more info and found a
    >lot on their usage, but not much on the internals of how they are
    >structured and operate. Thanks for any advice.


    The internal structure and operation (beyond what is specified for
    streams in general) is not subject of the C Standard.

    HTH a bit

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 29, 2003
    #4
  5. "Peter Pichler" <> wrote:

    >[...] all std* streams are unseekable.


    Hmm, I think it is implementation-defined behaviour.

    <OT>
    What about if input is redirected from a file, for example?
    </OT>

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 29, 2003
    #5
  6. Andrew

    Dan Pop Guest

    In <> Irrwahn Grausewitz <> writes:

    >"Peter Pichler" <> wrote:
    >
    >>[...] all std* streams are unseekable.

    >
    >Hmm, I think it is implementation-defined behaviour.


    Not even. The implementor cannot guess how I'm going to start the
    program ;-) After

    prog < input > output

    both stdin and stdout are seekable, if the input file exists and the
    output file can be created, on both Unix and Windows.

    The only thing guaranteed about the std* streams is that they are
    text mode streams (unless you freopen them differently).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 29, 2003
    #6
  7. (Dan Pop) wrote:

    >In <> Irrwahn Grausewitz <> writes:
    >
    >>"Peter Pichler" <> wrote:
    >>
    >>>[...] all std* streams are unseekable.

    >>
    >>Hmm, I think it is implementation-defined behaviour.

    >
    >Not even. The implementor cannot guess how I'm going to start the
    >program ;-)


    Right, I'll call that invocation-defined behaviour from now on. ;-)

    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 29, 2003
    #7
    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. Vincent Touquet
    Replies:
    1
    Views:
    630
    Adrian B.
    Sep 3, 2004
  2. Vincent  Touquet
    Replies:
    0
    Views:
    470
    Vincent Touquet
    Sep 6, 2004
  3. Replies:
    2
    Views:
    691
    velle
    Jan 5, 2006
  4. Guido De Rosa
    Replies:
    1
    Views:
    147
    Brian Candler
    Mar 9, 2010
  5. Snail
    Replies:
    7
    Views:
    177
    Anno Siegel
    Apr 9, 2005
Loading...

Share This Page