Article View: comp.lang.c.moderated
Article #2593fflush(stdin) - not guaranteed to work?
From: Himanshu Gohel
Date: Mon, 15 Apr 1996 04:37
Date: Mon, 15 Apr 1996 04:37
38 lines
2033 bytes
2033 bytes
In Paul S. Wang's "Introduction to ANSI C on UNIX" he says that
the fflush() function was "not intended to control input buffering"
on page 266.
My question is, why? Some of my students have written programs using
fflush(stdin) hoping it would clear away anything that's in the input
buffer before a scanf() statement and apparently it works on some PC
based compilers, but when re-compiled on a UNIX system, it does not
always work.
What is the best way to get rid of the '\n' from the input buffer after
a scanf() statement? In the same book on page 263 I've seen the following
format used in an fscanf() format specifier:
%*[\n]
which is explained as follows:
"...and %*[\n] consumes and discards the NEWLINE character at the end of
an input line."
But I've not had much luck with it when used as follows:
scanf("%c %*[\n]", &test);
The NEWLINE stays in the buffer and thus interferes with the next scanf()
statement. I am aware, however, of the " "%c" format which discards white
space before a character input, but my question is a more general one to
discard the NEWLINE.
If you do post to the newsgroup, please do send me a copy via e-mail too.
Thanks!
--
Himanshu Gohel, gohel@csee.usf.edu | You only have to do very few
WEB URL: http://www.csee.usf.edu/~gohel/ | things right in your life as
Geometric Modeling & Graphics Research Group | long as you don't do too many
U of South Florida, Tampa, FL. USA. | things wrong - Warren Buffett
Technical Details
Message-ID:
<4ksjpn$rjt@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: gohel@csee.usf.edu (Himanshu Gohel) Newsgroups: comp.lang.c.moderated,comp.lang.c Subject: fflush(stdin) - not guaranteed to work? Followup-To: comp.lang.c.moderated,comp.lang.c Date: 14 Apr 1996 23:37:11 -0500 Organization: Geometric Modeling and Computer Graphics Group, USF Tampa, FL. Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4ksjpn$rjt@solutions.solon.com> Reply-To: gohel@csee.usf.edu NNTP-Posting-Host: solutions.solon.com