linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jesse Ruffin <jesse@ajp-services.net>
To: linux-c-programming@vger.kernel.org
Subject: Re: Help: need to prevent infinite loop
Date: Mon, 23 Jan 2006 00:57:15 -0500	[thread overview]
Message-ID: <43D4703B.4030307@ajp-services.net> (raw)
In-Reply-To: <43D36C6D.4080500@ajp-services.net>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

The Standard I found, located at
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
does state in section 7.19.6.2 Sec. 9:

An input item is read from the stream, unless the specification includes
an n specifier. An input item is defined as the longest sequence of
input characters which does not exceed any specified field width and
which is, or is a prefix of, a matching input sequence (^245). The first
character, if any, after the input item remains unread. If the length of
the input item is zero, the execution of the directive fails; this
condition is a matching failure unless end-of-file, an encoding error,
or a read error prevented input from the stream, in which case it is an
input failure.

However, Footnote 245 says the following:

245) fscanf pushes back at most one character onto the input stream.
Therefore some sequences that are acceptable to strtod, strtol, etc. are
unacceptable to fscanf.

So, although it SHOULD be non-consumptive, it MAY not in all cases,
although I haven't found one where it doesn't yet.

The standard also recommended the following method for looping through
stdin:

#include <stdio.h>
/* ... */
int count; float quant; char units[21]; char items[21];
do {
	count = fscanf(stdin, "%f%20s of %20s", &quant, units, items);
	fscanf(stdin, "%*[^n]"); /* This junks all input remaining */
} while (!feof(stdin) && !ferror(stdin));
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD1HA78GGeAXLl3osRAuWHAKCAhXqVwMiQ2lC5NA+cdd1sg9g1vACZAZPi
HN0wUvcR8Hp2aw5PFmMJnVY=
=V96c
-----END PGP SIGNATURE-----

      reply	other threads:[~2006-01-23  5:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-17  4:36 Help: need to prevent infinite loop Shriramana Sharma
2006-01-17  7:14 ` Jesse Ruffin
2006-01-18 16:24   ` Shriramana Sharma
2006-01-18 16:33     ` Steve Graegert
2006-01-22 10:20   ` Glynn Clements
2006-01-22 11:28     ` Jesse Ruffin
2006-01-23  5:57       ` Jesse Ruffin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43D4703B.4030307@ajp-services.net \
    --to=jesse@ajp-services.net \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).