Solved

fflush

Posted on 2006-11-22
7
826 Views
Last Modified: 2011-10-03
in my code there is a part like

scanf("%c",&sex);
fflush(stdin);      
printf("Enter the choice (C/W)");
scanf("%c",&choice);
fflush(stdin);      


it works on windows; but on fedora5 it doesnot clear the cache, what can i do for this?
0
Comment
Question by:btocakci
7 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17999178
Hi btocakci,

This is a common problem when porting. The original coder made use of 'undefined' behaviour.

I dont know a concrete, simple route around this, there are many alternatives. Essentially you need to consume without blocking, all data until a '\n' is read. If you can trust your users not to attempt buffer overflow hacking, this method is the most trivial:

char trash[256];

...

scanf("%c%s",&sex,trash);
printf("Enter the choice (C/W)");
scanf("%c%s",&choice, trash);

It is certainly not my favourite but it can be done with a fairly simple search/replace.

A second approach could be something like:

#define flusstdin while ( ... ) {<Insert your consumer here.>}

Again, a simple search/replace can fix all the abuse then.

Paul
0
 
LVL 84

Expert Comment

by:ozo
ID: 17999562
fflush is only defined to work on output streams
some systems also define a non-standard fpurge that works on input streams
0
 
LVL 24

Accepted Solution

by:
fridom earned 63 total points
ID: 18001159
The better answer wouild be
use a combination of fgets and sscanf


char buf[SOME_LIMIT]
char * pc = fgets(buf, sizeof(buf), stdin)
if (! pc)
 // error handling
...
int vals_read
int val;
vals_read = sscanf(buf, "%d", &val);
if (vals_read != 1)
  // error handling


Regards
Friedrich
0
 

Expert Comment

by:karana
ID: 18006410

please scanf from new line


scanf("%c",&sex);

printf("Enter the choice (C/W)");
printf("\n");
scanf("%c",&choice);

     
0
 
LVL 2

Assisted Solution

by:Razor2k5
Razor2k5 earned 62 total points
ID: 18015064
Hi,

in order to have a more riguros approach use formatted read:

while (scanf("%d.%d.%d", &x, &y, &z) != 3) {
     while (getchar() != '\n'); /* line consuming
     printf("Try again...\n");
}
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 19
reading tzdatabase for timezone definitions 5 136
smtp c source code 7 69
Why is compiler in oracle server ? 9 66
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question