Fedora Linux Support Community & Resources Center
  #1  
Old 5th July 2006, 08:18 AM
mac23 Offline
Registered User
 
Join Date: Mar 2005
Location: Dibrugarh, Assam, India
Posts: 41
flushing input buffer

Is there any way to flush the input buffer? The problem where I faced it will make it clear what I want exactly.

Code:
main(){

	char buffer[20];
	
	scanf(“%s”, buffer);
	getchar();

}
Since the scanf does not take in the newline when the enter key is pressed, it remains in the buffer which is taken by getchar(); thus it does not wait for user input anymore. Of course this problem can be solved by replacing scanf with functions such as gets() or similar function as follows:-

Code:
main(){

	char buffer[20];
	
	gets(buffer);
	getchar();

}
But I want to know if there is any way to flush the input buffer. fflush () works for output stream not input; aren’t there any equivalent function for input stream?
__________________
Makarand Hazarika
NIT Durgapur
Reply With Quote
  #2  
Old 5th July 2006, 11:22 AM
abcde Offline
Registered User
 
Join Date: Jul 2006
Location: Brighton, England
Age: 24
Posts: 2
Quote:
Originally Posted by mac23
Is there any way to flush the input buffer? fflush () works for output stream not input; aren’t there any equivalent function for input stream?
If I want to zap to a newline, I getchar() all the characters until I get EOF or a newline:

Code:
int main() {

	char buffer[20];
	int ch;
	
	scanf(“%s”, buffer);
	while ((ch = getchar()) && ch != '\n' && ch != EOF) { /* do nothing */ }

}
Quote:
Originally Posted by mac23
Of course this problem can be solved by replacing scanf with functions such as gets()
Just a warning: gets() is dangerous (re: buffer overflows) and you should get a warning if you use it. Use fgets() as it is safe.
__________________
Oh, frabjous day! Calloo, callay!
Reply With Quote
  #3  
Old 6th July 2006, 07:14 AM
sheep Offline
Registered User
 
Join Date: Jun 2006
Posts: 92
Code:
#include <stdio.h>

int main(int argc, char **argv)
{
        char buf[256];
        fscanf(stdin, "%s", buf);
        return 0;
}
Do NOT use gets(). EVER.

Also: http://c-faq.com/stdio/stdinflush.html and this: http://c-faq.com/stdio/stdinflush2.html .

Last edited by sheep; 6th July 2006 at 07:17 AM.
Reply With Quote
  #4  
Old 8th July 2006, 07:01 PM
mac23 Offline
Registered User
 
Join Date: Mar 2005
Location: Dibrugarh, Assam, India
Posts: 41
thanks to both of u. indeed i used to get a warning that gets is dangerous.
__________________
Makarand Hazarika
NIT Durgapur
Reply With Quote
Reply

Tags
buffer, flushing, input

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fetchmail flushing messages stry_cat Using Fedora 0 4th March 2009 07:12 PM
clear terminal input buffer? Chilly Willy Using Fedora 4 26th December 2008 12:00 AM
fc7 hangs on Input: PS/2 Generic Mouse as /class/input/input2 spectre_man Installation and Live Media 2 2nd January 2008 01:45 AM
IDE disk flushing with halt in F7 Lev Hardware & Laptops 0 5th June 2007 12:45 AM


Current GMT-time: 05:40 (Monday, 20-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat