Register to reply

C : flushing input stream..

by !kx!
Tags: flushing, input, stream
Share this thread:
!kx!
#1
Jan13-10, 01:56 AM
P: 58
hi..
i need to ask something related to c programming.. (and i don't know if this is the right place to put up this question.. so please redirect me if necessary!)

i've read that using fflush(stdin) in C (to flush the input stream) is not a defined way to use fflush..
but, i continue to see it being used successfully in various instances.. please tell how and why it works..?
Phys.Org News Partner Science news on Phys.org
Mysterious source of ozone-depleting chemical baffles NASA
Water leads to chemical that gunks up biofuels production
How lizards regenerate their tails: Researchers discover genetic 'recipe'
hamster143
#2
Jan13-10, 03:51 AM
P: 986
fflush(stdin) has the effect of removing any data sitting in stdin's buffer at the moment of the call.
!kx!
#3
Jan13-10, 04:48 AM
P: 58
if it does that without any problem.. then why it is not advisable to use fflush() on input streams..??

D H
#4
Jan13-10, 06:00 AM
Mentor
P: 15,152
C : flushing input stream..

Quote Quote by hamster143 View Post
fflush(stdin) has the effect of removing any data sitting in stdin's buffer at the moment of the call.
It might have that effect -- on your computer with your compiler. What effect it will have elsewhere, who knows?

From the C standard (emphasis mine),
int fflush(FILE *ostream);

ostream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment to
be written to the file; otherwise, the behavior is undefined.

Intentionally invoking undefined behavior is a very, very bad idea.
!kx!
#5
Jan13-10, 06:03 AM
P: 58
ok..

and what happens to the data that is removed from the stream.. is it simply deleted.. or something else?
D H
#6
Jan13-10, 06:16 AM
Mentor
P: 15,152
Who knows? What fflush does when applied to an input stream is undefined. What that means is that the compiler developers are free to do anything they please in response. fflush(stdin) might
  • Throw away any characters that happen to be in the input buffer,
  • Do absolutely nothing,
  • Cause your program to crash, or even
  • Make your computer emit nasal demons.
!kx!
#7
Jan13-10, 06:18 AM
P: 58
no i meant.. what will fflush() do to the data in output stream, if applied to it..
!kx!
#8
Jan13-10, 06:19 AM
P: 58
and what are 'nasal demons' ?!!
D H
#9
Jan13-10, 06:32 AM
Mentor
P: 15,152
Quote Quote by !kx! View Post
no i meant.. what will fflush() do to the data in output stream, if applied to it..
Different question! fflush() when applied to an output stream causes any buffered output to be sent to the output device. IO devices are very, very slow compared to memory. A program that uses unbuffered output often runs much slower than a similar program that uses buffered output. In unbuffered output, characters are immediately sent to the output device. In buffered output, the output handler stores output data and later sends the buffered data to the output device en masse. Typically this happens when the output buffer becomes full or when some special character such as a newline is sent. You as a programmer have some control over how the buffering works via the function setvbuf().
D H
#10
Jan13-10, 06:35 AM
Mentor
P: 15,152
Quote Quote by !kx! View Post
and what are 'nasal demons' ?!!
Click on that term. It is a hyperlink; you can see that by the underlining and the fact that your cursor changes from a normal arrow to a hand with a finger when you move your mouse over the text.
!kx!
#11
Jan13-10, 08:30 AM
P: 58
yeah.. sorry.. didn't see the hyperlink!!
and thnx for all that help..
hamster143
#12
Jan13-10, 01:08 PM
P: 986
Quote Quote by D H View Post
Who knows? What fflush does when applied to an input stream is undefined. What that means is that the compiler developers are free to do anything they please in response.
Strictly speaking, that's up to standard library developers rather than compiler developers. MSVC standard library and any others compatible with it (such as Intel library for Windows) do what I said: wiping all pending data from the input buffer. GCC standard library (glibc) does nothing and returns an error.

As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.
!kx!
#13
Jan13-10, 07:51 PM
P: 58
[As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.[/QUOTE]

can u please shed some light on what a stream is in general... and what are different types of 'em..?
D H
#14
Jan13-10, 10:15 PM
Mentor
P: 15,152
Quote Quote by hamster143 View Post
Strictly speaking, that's up to standard library developers rather than compiler developers.
Strictly speaking, yes. Practically speaking, no. C compilers almost always come bundled with a library.

MSVC standard library and any others compatible with it (such as Intel library for Windows) do what I said: wiping all pending data from the input buffer. GCC standard library (glibc) does nothing and returns an error.
That's because the response to fflush(stdin) is undefined. What that means is that implementors of fflush are free to do whatever they wish when passed an input stream (or an update stream on which the last operation was input) and remain compliant with the standard. Programmers are strongly recommended to avoid undefined behavior.

As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.
O rly?

stdin is the standard input stream. Using it as an output stream is once again undefined.
hamster143
#15
Jan13-10, 11:24 PM
P: 986
stdin is the standard input stream. Using it as an output stream is once again undefined.
It is initially opened as an input stream. There's nothing to keep you from calling freopen("filename", "wb", stdin), or, on many platforms, even doing "stdin = stdout; "!


Register to reply

Related Discussions
When a stream hits a surface, why does the stream always split in two? General Physics 1
Electrical and Computer Engineering, 2-input Look-Up tables designed with 2-input XOR Engineering, Comp Sci, & Technology Homework 0
How to obtain 4 input or gate using 2 input gates? Introductory Physics Homework 3
A stream of water Introductory Physics Homework 14
Logic question, 3 input OR to 2 input OR Electrical Engineering 9