Substitute for gets()

MJSchmelzer xmjschm at hscfvax.harvard.edu
Wed Feb 8 15:00:23 AEST 1989


Given that gets() is unsafe because it doesn't check its input,
what form of scanf() should be used?

The problem I'm having is this:

scanf("%s", buffer);

where buffer is char[255] and the scanf statement is in a loop,
the \n gets passed to the next scanf call. 
Another problem is if I enter, say:

foo bar

to this scanf-in-a-loop, it cycles through the stdin buffer twice to yield:

foo
bar

I know I'm not saying this very well, sorry. But I figure this is a pretty
basic dilemma that most folks have encountered. I mean, gets() in such a 
loop does just fine, ie it would return "foo bar", but I have yet to
kludge up a functionally equivalent scanf().

Does this mean gets() is indispensable ? 
Thanks for any help/insights.
 
-- 
==============  xmjschm at harvspha.BITNET ============= 
Mike Schmelzer  xmjschm at hscfvax.harvard.edu           Quotes are stupid.
=====================================================



More information about the Comp.lang.c mailing list