Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!seismo!beno!black From: black@beno.CSS.GOV (Mike Black) Newsgroups: comp.lang.c Subject: Re: problems with scanf() and gets() Summary: Don't use scanf(), fscanf(), or gets() Message-ID: <49683@seismo.CSS.GOV> Date: 6 Jun 91 22:55:14 GMT References: <2759@apss.apss.ab.ca> <1991Jun6.182118.16211@ux1.cso.uiuc.edu> Sender: usenet@seismo.CSS.GOV Distribution: comp Organization: Center for Seismic Studies, Arlington, VA Lines: 31 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@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!: -------------------------------------------------------------------------------