LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 30
Streams and buffering problem


Hi,

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.

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

Rep: Reputation: 34
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, 01:32 PM   #3
MadCactus
Member
 
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?
 
  


Reply


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 04:46 AM
pipeline buffering jk3us Linux - Software 2 12-17-2004 07:52 PM
turn off buffering suchi_s Programming 2 10-30-2004 07:14 AM
Regarding buffering with streams karthikvina Programming 0 09-04-2003 02:48 AM
frame buffering problem. GT-GEO Linux - General 3 01-31-2002 04:26 AM


All times are GMT -5. The time now is 05:54 PM.

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