Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stdin and stdout

Reply
Thread Tools

stdin and stdout

 
 
Andrew
Guest
Posts: n/a
 
      10-28-2003
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.
 
Reply With Quote
 
 
 
 
Peter Pichler
Guest
Posts: n/a
 
      10-28-2003
"Andrew" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-28-2003
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-29-2003
(E-Mail Removed) (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
((E-Mail Removed))
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-29-2003
"Peter Pichler" <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-29-2003
In <(E-Mail Removed)> Irrwahn Grausewitz <(E-Mail Removed)> writes:

>"Peter Pichler" <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-29-2003
(E-Mail Removed) (Dan Pop) wrote:

>In <(E-Mail Removed)> Irrwahn Grausewitz <(E-Mail Removed)> writes:
>
>>"Peter Pichler" <(E-Mail Removed)> 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
((E-Mail Removed))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
daemonize, redirect stdin and stdout and write a pidfile Guido De Rosa Ruby 1 03-09-2010 10:07 AM
Getting the encoding of sys.stdout and sys.stdin, and changing it properly velle@velle.dk Python 2 01-05-2006 11:33 AM
Start a program and get a hold of it's STDOUT and STDIN? Snail Perl Misc 7 04-09-2005 11:19 AM
Re: embedding Python and changing stdin, stdout and stderr Vincent Touquet Python 0 09-06-2004 04:35 PM
embedding Python and changing stdin, stdout and stderr Vincent Touquet Python 1 09-03-2004 12:25 PM



Advertisments