Solved

fflush

Posted on 2006-11-22
7
803 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now