Unix problem or C problem?

Brandon S. Allbery allbery at ncoast.ORG
Mon Feb 20 02:58:25 AEST 1989


As quoted from <18366 at adm.BRL.MIL> by rbj at nav.icst.nbs.gov (Nilbert T Bignum):
+---------------
| ? As an aside, I generally avoid testing scanf() values against EOF. scanf()
| ? returns the number of successful assignments (1 in this case), so you may
| ? be a lot safer saying: ...
| 
| I agree. In fact, I would avoid testing the return value of scanf or
| fscanf against *anything*. Don't use them at all. Use fgets followed
| by sscanf. If you don't like the input on a particular line, it is
| easier to throw it away by doing another fgets than by constructing
| a scanf expression to read `any string terminated by newline but only
| up to this many characters into that junk buffer over there'. On another
| point, you may want to rescan the input somehow.
+---------------

Using scanf() for interactive input has another wart:  if the (l)user
doesn't enter exactly the number of data items requested, s/he won't be
prompted properly for further input.  And it's (almost) impossible to catch
malformed data files in the non-interactive case.

++Brandon
-- 
Brandon S. Allbery, moderator of comp.sources.misc	     allbery at ncoast.org
uunet!hal.cwru.edu!ncoast!allbery		    ncoast!allbery at hal.cwru.edu
      Send comp.sources.misc submissions to comp-sources-misc@<backbone>
NCoast Public Access UN*X - (216) 781-6201, 300/1200/2400 baud, login: makeuser



More information about the Comp.unix.wizards mailing list