Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 4th, 2005, 8:47 AM   #1
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
Question Clearing STDIN

Hi,
I am making a simple database program in C right now, and I have one problem. I need to know if there is any function that deletes the content of STDIN???
Thanks for help.
ivan is offline   Reply With Quote
Old Oct 4th, 2005, 8:53 AM   #2
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
if you mean to flush stdin, then use
rewind(stdin);
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Oct 4th, 2005, 2:26 PM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
rewind(stdin) is not really defined for stdin when it is a terminal or a pipe.- rewind is the same as
 fseek (stream, 0L, SEEK_SET)
which is essentially meaningless for a tty.

The usual approach is a platform-specific hunk of code. Some wierd compilers actually define fflush(stdin) as valid - this is not standard though because the standard says that fflush(stdin) has undefined behavior.

What platform are you on Windows? Linux?
jim mcnamara is offline   Reply With Quote
Old Oct 4th, 2005, 2:34 PM   #4
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
[php]
#include <stdio.h>

#define clear_stdin() while(getc(stdin) != '\n')

int main(int argc, char* argv)
{
char bleh = getc(stdin);
printf("%c\n", bleh);
clsStdin();
bleh = getc(stdin);
printf("%c\n", bleh);
return 0;
}
[/php]

Output:

i am testing
i
hello world
h
__________________

tempest is offline   Reply With Quote
Old Oct 4th, 2005, 2:37 PM   #5
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
Quote:
Originally Posted by jim mcnamara
What platform are you on Windows? Linux?
I have windows xp.
ivan is offline   Reply With Quote
Old Oct 4th, 2005, 2:41 PM   #6
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
tempest that's not working.
clsStdin() is an undefined reference.

Otherwise, my compiler is Dev-C++
ivan is offline   Reply With Quote
Old Oct 4th, 2005, 2:48 PM   #7
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5 Benoit is on a distinguished road
Wouldn't you just use fflush(stdin); ?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 4th, 2005, 2:52 PM   #8
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by ivan
tempest that's not working.
clsStdin() is an undefined reference.

Otherwise, my compiler is Dev-C++
i think he made a typo, since he has defined the macro clear_stdin at the top. He meant clear_stdin instead of clsStdin.
Polyphemus_ is offline   Reply With Quote
Old Oct 4th, 2005, 2:58 PM   #9
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Yea, my bad... i compiled it one way and then changed it for asthetics and forgot to change both parts...
__________________

tempest is offline   Reply With Quote
Old Oct 4th, 2005, 3:09 PM   #10
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
Both fflush( stdin ) and rewind( stdin ) are working.
What the difference then and which one should I use?
ivan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC