problems with scanf() and gets()

Mike Black black at beno.CSS.GOV
Fri Jun 7 08:55:14 AEST 1991


NEVER USE THESE FUNCTIONS!

1. scanf() and fscanf() - if you don't input the EXACT format you
are scanning for, your input stream will no proceed any further.
i.e. scanf("%d",&i) will stop when you type "hello" and will
not scan blasted thing until you read in the hello with another
function.

2. gets() - It doesn't check to make sure you don't overrun the 
char array you pass it.

INSTEAD USE THESE!

1. fgets() followed by sscanf()  - This gives you complete control
over your input stream by allowing you to discard garbage.  i.e.

char buf[100];
fgets(buff,100,stdin);
if(sscanf(buf,"%d",&i) != 1) puts("You typed the wrong thing!");

2. fgets() - it's the same as gets with 2 more arguments, the
main one of which is the maximum length of your array.

One of the other benefits of using fgets() and sscanf() on numeric
input is it's generally quite faster than fscanf().  Try it!
Mike...
--
-------------------------------------------------------------------------------
: usenet: black at beno.CSS.GOV   :  land line: 407-494-5853  : I want a computer:
: real home: Melbourne, FL     :  home line: 407-242-8619  : that does it all!:
-------------------------------------------------------------------------------



More information about the Comp.lang.c mailing list