home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.programming
- Subject: Re: HELP with scanf/getchar!
- Message-ID: <27401@dog.ee.lbl.gov>
- Date: 12 Nov 92 12:30:05 GMT
- References: <1992Nov9.162547.11163@seas.smu.edu> <1992Nov9.205548.5984@bcrka451.bnr.ca> <s1110238.721534396@giaeb>
- Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
- Followup-To: comp.lang.c
- Organization: Lawrence Berkeley Laboratory, Berkeley
- Lines: 38
- NNTP-Posting-Host: 128.3.112.15
-
- I was going to stay out of this; the original question should have been
- sent to comp.lang.c, not comp.programming.
-
- In article <s1110238.721534396@giaeb> s1110238@giaeb.cc.monash.edu.au
- (Lee Hollingworth) provides a workable (if a bit buggy; it will loop
- forever if EOF occurs without a newline). solution. Along the way to
- that particular article (see references: line), however, a lot of
- incorrect advice went out.
-
- The original problem was caused by the call
-
- scanf("%d\n", ...)
-
- In the C language, this means to read through standard input, as follows:
-
- - skip any leading white space, including newlines;
-
- - read and convert an integer, storing the result through the
- pointer provided; and (here comes the problem)
-
- - skip any further white space, including newlines.
-
- The last is a result of the `\n' directive. It does not just skip
- `current' white space, but also `future' white space. In order to get
- the future white space, it will read the next line (and any subsequent
- lines as needed) until it encounters EOF or a non-white-space character.
-
- This may seem like a bizarre thing to do. Well, it is. On the whole,
- C's scanf() formats are not particularly well designed. The
- comp.lang.c FAQ advises against direct calls to scanf(): line-at-a-time
- input should generally be done with calls to fgets followed by calls to
- sscanf.
-
- Further discussion of C's scanf() belongs in comp.lang.c (but please
- read the FAQ first!). Note that I have redirected followups.
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-