LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 01-25-2013, 05:38 AM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
Ncurses/C how to clear (stdin?) buffer?


hey I have wgetch() in a loop and it works great accept when there is
five inputs. My loop will execute five times to receive all the input.
I was wondering if there is a way to clear the linux input buffer
right before my call to wgetch(); so that all the previous commands
while scrolling text to the screen are disregarded.

Like during a loop, if the user enters a key, When there prompted to enter a key it will act as though they entered one because if I am not mistaken the input was queued so i want to stop that temperarly and then restart that later.

Last edited by errigour; 01-25-2013 at 05:43 AM.
 
Old 01-28-2013, 08:13 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
This may or may not be helpful but I found the function below several years ago (can't remember exactly where) and used it quite happily in a toy app that was an attempt to write safe c:

Code:
/*
	flushes STDIN in a defined fashion rather than 'fflush(stdin)' which is undefined in the C standard
	N.B. only use after testing for excess chars, e.g
	if (count >= maxChars) { 
	flushSTDIN();
	}
	otherwise if the buffer is empty it will wait for input.
*/

void flushSTDIN() {

int c;
do { 
errno = 0;
c = getchar();

	if (errno) { 
		fprintf(stderr, "Error flushing standard input buffer: %s\n", strerror(errno)); 
	} 
}
while ((c != '\n') && (!feof(stdin)));

return; 
}
 
  


Reply



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
flushing perl STDIN buffer? Daravon Programming 8 05-21-2012 05:06 PM
Exceed on Demand client doesn't clear buffer sharky Linux - Software 0 02-03-2010 10:37 AM
Clear Print Buffer? FlameKhan General 2 03-15-2005 02:29 AM
How do I clear less' buffer? hussar Slackware 7 12-24-2004 04:44 PM
get character without enter/stdin-buffer lea Programming 3 09-07-2003 03:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration