[comp.lang.c] stdin

jdb@reef.cis.ufl.edu (Brian K. W. Hook) (12/28/90)

I want to put a key back into the keyboard buffer used by getchar and/or 
getc.  Now, I was wondering (other than using ungetc()), is it possible
to do the following:

freopen(stdin,"a");

?

As far as I can tell, stdin is a stream written for read access only or
something...can I reopen it and append to it?  Just wondering.

Brian

dgil@pa.reuter.COM (Dave Gillett) (01/04/91)

In <26089@uflorida.cis.ufl.EDU> jdb@reef.cis.ufl.edu (Brian K. W. Hook) writes:

>I want to put a key back into the keyboard buffer used by getchar and/or 
>getc.  Now, I was wondering (other than using ungetc()), is it possible
>to do the following:

>freopen(stdin,"a");

>As far as I can tell, stdin is a stream written for read access only or
>something...can I reopen it and append to it?  Just wondering.


     Well, besides the fact that ungetc() already exists and is probably
optimized by your library provider, consider the fact that stdin is a stream.
In this special case, its "append end" is already open by the system and 
(virtually) connected to the keyboard or some input file.  In either case,
there can be lots more characters "in" stdin after the one you just decided
you shouldn't have read.
     If you *could* go and append the character to the stream, it would appear
*after* all of the other characters already in the stream.  If stdin is
connected to a keyboard, this might not be very many, but even then you are
not allowed to assume that it is none.

     If you don't want your output to be a stream, don't use a stream.  Get
your keyboard input from the system-supplied keyboard routines and bid
portability adieu.  If you'd rather be portable and use streams, then
treat them as streams and forget implementation details like keyboards
and buffers.
                                                      Dave