stdin and stdout

A

Andrew

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

Peter Pichler

Andrew said:
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
 
E

Eric Sosman

Andrew said:
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.
 
I

Irrwahn Grausewitz

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
 
I

Irrwahn Grausewitz

Peter Pichler said:
[...] 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
 
D

Dan Pop

In said:
Peter Pichler said:
[...] 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top