Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #12320

Re: no error by fscanf on reading from output file

From Ike Naar <ike@iceland.freeshell.org>
Newsgroups comp.lang.c
Subject Re: no error by fscanf on reading from output file
Date 2011-10-31 07:02 +0000
Organization A noiseless patient Spider
Message-ID <slrn3vfsjasi0u.447.ike@iceland.freeshell.org> (permalink)
References <9b198cc9-d374-4a14-a100-91b74eb12d9e@a12g2000prb.googlegroups.com> <88a48804-4b47-41f8-bd12-e5b8b4c7df85@m19g2000vbm.googlegroups.com>

Show all headers | View raw


On 2011-10-31, Carlo Dapor <catull@gmail.com> wrote:
> 2. Format used
>
> You assumed that two numbers are adjacent ("%d%d").

No. "%d%d" is a valid format for reading two separated integers;
it will skip initial whitespace before the first integer, read the
first integer, skip whitespace beteeen the first and the second
integer, and read the second integer.

> This is rather misleading.
> Say the input were 193.
> Should that be read as "1" followed by "93" (case 1), or "19" followed
> by "3" (case 2).

Neither; it will read the first number as 193 (assuming the next
character after "193" does not extend that number to a larger valid
integer).  Then any separating whitespace is skipped, and a second
integer will be read. If there is no second integer in the input
stream, fscanf will still read the first integer as 193, then return
1 to indicate that only one item was read.

> The solution is to specify "%1d%2d" for case 1 and "%2d%1d" for case
> 2.

In general you do not know beforehand how large the numbers in the
input stream will be, so you cannot predict the number of digits
to read for the first number.  Separating the numbers by whitespace
is often the better way.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

no error by fscanf on reading from output file "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-29 23:59 -0700
  Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-30 20:19 +1300
    Re: no error by fscanf on reading from output file Ike Naar <ike@iceland.freeshell.org> - 2011-10-30 08:18 +0000
      Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-30 21:23 +1300
        Re: no error by fscanf on reading from output file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-30 08:11 -0400
          Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 07:38 +1300
            Re: no error by fscanf on reading from output file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-30 19:40 +0000
              Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 08:50 +1300
  Re: no error by fscanf on reading from output file "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-30 00:01 -0700
  Re: no error by fscanf on reading from output file Ike Naar <ike@iceland.freeshell.org> - 2011-10-30 08:39 +0000
  Re: no error by fscanf on reading from output file Carlo Dapor <catull@gmail.com> - 2011-10-30 19:16 -0700
    Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 15:50 +1300
      Re: no error by fscanf on reading from output file "V.Subramanian, India" <subramanian100in@yahoo.com> - 2011-10-30 22:55 -0700
        Re: no error by fscanf on reading from output file pacman@kosh.dhis.org (Alan Curry) - 2011-10-31 07:14 +0000
          Re: no error by fscanf on reading from output file Ian Collins <ian-news@hotmail.com> - 2011-10-31 20:56 +1300
          Re: no error by fscanf on reading from output file Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-31 15:43 +0000
        Re: no error by fscanf on reading from output file Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-10-31 01:35 -0700
          Re: no error by fscanf on reading from output file Keith Thompson <kst-u@mib.org> - 2011-10-31 13:46 -0700
    Re: no error by fscanf on reading from output file Ike Naar <ike@iceland.freeshell.org> - 2011-10-31 07:02 +0000

csiph-web