flushing the standard input stream (newb question) 
Author Message
 flushing the standard input stream (newb question)

I tried fflush(stdin) but it doesn't work.  I'm using metroworks
codewarrior on a mac.


Mon, 13 Sep 2004 14:24:36 GMT  
 flushing the standard input stream (newb question)

Quote:

> I tried fflush(stdin) but it doesn't work.  I'm using metroworks
> codewarrior on a mac.

It's not supposed to work.  The behavior of fflush(stdin) is
undefined.  See the FAQ:

12.26:  How can I flush pending input so that a user's typeahead isn't
        read at the next prompt?  Will fflush(stdin) work?

A:      fflush() is defined only for output streams.  Since its
        definition of "flush" is to complete the writing of buffered
        characters (not to discard them), discarding unread input would
        not be an analogous meaning for fflush on input streams.

        There is no standard way to discard unread characters from a
        stdio input stream, nor would such a way necessarily be
        sufficient, since unread characters can also accumulate in
        other, OS-level input buffers.  You may be able to read and
        discard characters until \n, or use the curses flushinp()
        function, or use some system-specific technique.  See also
        questions 19.1 and 19.2.

        References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.



Mon, 13 Sep 2004 14:26:01 GMT  
 flushing the standard input stream (newb question)

Quote:

> I tried fflush(stdin) but it doesn't work.  I'm using metroworks
> codewarrior on a mac.

Of course not.  fflush is only defined for output files.  Try:

int flushln(FILE *f)
{
   int ch;

   while (('\n' != (ch = fgetc(f)) && (EOF != ch))) continue;
   return ch;

Quote:
}

which will gobble up another line if you have already read the
line ending \n before calling it.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Mon, 13 Sep 2004 17:16:08 GMT  
 flushing the standard input stream (newb question)

Quote:

> I tried fflush(stdin) but it doesn't work.  I'm using metroworks
> codewarrior on a mac.

What do you thin "works" should mean?  fflush(), defined only for
streams open for output or last used for output, means to complete the
action of pending writes.  What do you imagine this would mean on an
input stream, and why?

--
It is better that the grammarians should chide us
than that the people should not understand us.
  - St. Augustine



Mon, 13 Sep 2004 18:33:21 GMT  
 flushing the standard input stream (newb question)

Quote:
>> I tried fflush(stdin) but it doesn't work.  I'm using metroworks
>> codewarrior on a mac.

>What do you thin "works" should mean?  fflush(), defined only for
>streams open for output or last used for output, means to complete the
>action of pending writes.  What do you imagine this would mean on an
>input stream, and why?

fflush() on an input stream referring to a keyboard should force the
user to untype the characters that have not been read.  This is extremely
dangerous if the user is not at the keyboard but is engaged in some
other activity such as driving, sleeping, or having sex.  Expect to get
sued for using this, especially at the workplace.

                                        Gordon L. Burditt



Tue, 14 Sep 2004 08:07:22 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Flushing input streams

2. newbie question about input stream buffer

3. c++ standard streams question

4. Making sure that a stream is fully flushed

5. Quick C Stream Flushing

6. flushing a stream when file can't be written

7. Making sure a file stream gets fully flushed

8. Standard method for getting string input ?(newbie question)

9. flush the input buffer

10. Flushing input buffers

11. Still Stumped with Flushing Input Buffer :(

12. flushing input.....

 

 
Powered by phpBB® Forum Software