home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!bart!volpe
- From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
- Newsgroups: comp.lang.c
- Subject: Re: ** Ack! Help again! **
- Message-ID: <1993Jan12.132507.19155@crd.ge.com>
- Date: 12 Jan 93 13:25:07 GMT
- References: <kaNTwB1w165w@tradent.wimsey.bc.ca> <dschmitz.726261452@pv0321.vincent.iastate.edu> <841@ulogic.UUCP>
- Sender: volpe@bart (Christopher R Volpe)
- Reply-To: volpe@ausable.crd.ge.com
- Organization: GE Corporate Research & Development
- Lines: 53
- Nntp-Posting-Host: bart.crd.ge.com
-
- In article <841@ulogic.UUCP>, hartman@ulogic.UUCP (Richard M. Hartman) writes:
-
- [example code showing use of gets after scanf to read up to next newline
- deleted.]
-
- |> There should be no need for such a hack.
- |> In your scanf(), does the format string contain a "\n"?
- |> Try this:
- |>
- |> /* test.c */
- |> #include <stdio.h>
- |> main()
- |> {
- |> int i; char buf[81];
- |>
- |> fflush(stdin);
- |> scanf("%d", &i);
- |> gets(buf);
- |> printf("[%d], [%s]\n", i, buf);
- |>
- |> fflush(stdin);
- |> scanf("%d\n", &i);
- |> gets(buf);
- |> printf("[%d], [%s]\n", i, buf);
- |> }
- |>
- |> log of run:
- |>
- |> $ ./test
- |> 23
- |> [23], []
- |> 132
- |> fsdljk
- |> [132], [fsdljk]
- |> $
- |>
-
- Have you tried this with non-trivial input? Your assumption is wrong. A
- newline in the format string will not force a read up to the next newline
- in the input. A newline is whitespace, and therefore will simply
- consume whatever whitespace exists before the next field. It will not
- skip over superfluous fields on the same line, as the code posted by the
- previous poster would.
-
- However, I would use fgets followed by sscanf, rather than scanf followed
- by gets. It's safer.
-
- -Chris
- --
- ==================
- Chris Volpe
- G.E. Corporate R&D
- volpecr@crd.ge.com
-