Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fflush problem 1

Status
Not open for further replies.

smaniraja

Programmer
Feb 19, 2001
129
IN
#include <stdio.h>
int main()
{
char a, b;
a = getchar();
fflush(stdin);
b = getchar();
printf(&quot;........The Result are a: %c b : %c&quot;, a, b);
return 0;
}

Hi all see the program. while reading a character for the variable b its giving problems. The fflush is not clearing the input buffer (stdin).

How to solve this.
 
The standard input is actually buffered so what happens is u would
be pressing some character and then enter only that character is
assigned to a and the enter value remains in the queue(buffer). so
it gets assigned to b since it is a non-printable character u may find
b to be empty print b in decimal format u would find value.
try it out .

sramki
 
Yes I know that whatever we are giving as a input that will be stored in the keyboard buffer [queue]. The getchar() will take characters from that queue.

we have to type a character and then press the Enter key for the first getchar() to accept a character. The first character input and Enter key will be stored in the keyboard buffer.

The getchar() function [ or any function to read a character ] will get characters from keyboard only when the keyboard buffer is free. In this case the, while reading the a character for the variable b the Enter key is stroed in the keyboard buffer and its not empty, so the b = getchar() functions takes the Enter key from the buffer and its not reading any characters from the keyboard.

My question is about clearing the keyboard buffer. If I remove the characters stroed in the buffer before reading a character for variable b then it will read a character from the keyboard.

Normally fflush(stdin) will do this, thus what I gave that statement before the b = getchar() function call, but the fflush function call is not removing the characters in the buffer. I don't know why ?.

so, My question is why the fflush(stdin) statement is not clearing the keyboard buffer[ I am getting this Unix/Linux only, the same is working in dos and windows].

Regards
Maniraja S
 
Hi smaniraja,

I could run your program correctly
on a Solaris Sparc(2.7)

abp :cool:
 
Hi abp,

Thanks, but I am having the problem in Linux [ Red Hat Linux release 6.2 ]

Regards
Maniraja S
 
In C (and I believe for C++ as well), fflush() is only defined for output buffers, so there is no guarantee that fflush(stdin) will remove all the pending characters in the input buffer. It's up to the implementation as to how to handle input buffers. Many discard the characters in the input buffer (MS even documents this behavior for MSVC but fails to mention the behavior is not defined by the C standard and shouldn't be relied on across implementations) while others do nothing. An implementation would even be conforming if it crashed your program when encountering an fflush(stdin).

If you think about it, the behavior of fflush() doesn't even make sense for input buffers since fflush() doesn't discard characters, but rather removes them from the buffer and immediately writes them to the associated stream. Where should fflush() write characters in an input buffer?

One alternative is to write your own function that discards input characters. Something like this:

int discard_input(void)
{
int c;
while ((c=getchar())!=EOF && c!='\n') ;
return c==EOF;
}

Russ
bobbitts@hotmail.com
 
Hi,

I need help on buffer cleaning.

What I mean is:
Once I fill the buffer with
Code:
strcpy(buff,&quot;xyz\r&quot;)
and next time I need the
Code:
buff
to be empty with nothing inside it. How do I do that?

I work on LabWindows/CVI (a GUI based C environment) and I don't think it supports all C library functions like
Code:
fflush()
.

Actually, first time I need to write
Code:
xyz
in the buffer and next time only
Code:
xy
and then I need to compare the buffer with an ASCII character 'ACK'. So, if the buffer is not cleaned I cann't compare the two.

Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top