Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-16-2004, 01:04 PM   #1
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Rep: Reputation: 30
Streams and buffering problem


I have this problem in my code where I scanf a %d value, and if it is not converted I want to flush stdin and return to the main menu where a choice is presented.

Problem is that if the error condition occurs (say I enter "eep"), I return to the main menu with those characters on the stream and so scanf picks them up, and since 'e' is one of the menu options the case statement is triggered.

I thought that fflush(stdin) would do the trick, but apparently on GNU libc this only causes the contents of the buffer to be written to the stream.

Now I don't know much about buffered I/O, but i'm pretty sure what I want to do is also clear the stream somehow so subsequent calls to scanf don't pick up stray chars.

Hope someone can help with this.

Old 02-16-2004, 02:41 PM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
The C standard for fflush() is to flush OUTPUT streams only.
The result is undefined for input streams. i.e., it may or may not do anything.
Old 02-17-2004, 02:32 PM   #3
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195

Original Poster
Rep: Reputation: 30
I found that theres a function __fpurge(FILE *stream) (from stdio_ext.h) that does exactly what I want, ie purges the buffer and the stream like a good enema.

However I think (but am not sure) thats only with GNU libc - i'd like something a bit more portable if possible.

Anyone know about that?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Kaffeine always Buffering AvatarofVirgo Linux - General 1 10-13-2005 05:46 AM
pipeline buffering jk3us Linux - Software 2 12-17-2004 08:52 PM
turn off buffering suchi_s Programming 2 10-30-2004 08:14 AM
Regarding buffering with streams karthikvina Programming 0 09-04-2003 03:48 AM
frame buffering problem. GT-GEO Linux - General 3 01-31-2002 05:26 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration