On 07/08/2013 05:12 AM, Nobody wrote:
....
fgets()+sscanf() is appropriate when newlines are significant, i.e. each
"chunk" of input is on its own line. If the input contains too few values,
fgets()+sscanf() will parse one line of input and tell you how many
values it obtained, whereas fscanf() will start reading values from the
next line().
fscanf() is appropriate when newlines are merely whitespace ...
True
... (e.g. as is
the case for C source code).
Not true. In phase 7, "White-space characters separating tokens are no
longer significant", which renders new-line characters outside of tokens
insignificant (new-line characters can occur inside of character
constant and string literal tokens as a result of translation of \n
escape sequences during phase 6). However, many features of C which are
implemented prior to phase 7 depend upon correctly distinguishing
between new-line characters and other white-space characters:
Phase 2: "Each instance of a backslash character (\) immediately
followed by a new-line character is deleted, splicing physical source
lines to form logical source lines."
Phase 3:
* It is a syntax error for a header name, character constant, or string
literal to contain a new-line character.
Phase 4:
* The # character that distinguishes pre-processing directives must be
the first non-whitespace character after a new-line character or the
start of the source code file.
* Preprocessing directives are terminated by new-line characters - even
if doing so would interrupt what would otherwise be the invocation of a
function-like macro.
* __LINE__ must expand into 1 more than then number of new-line
characters were read during translation phase 1 at the point where that
token is parsed.
* assert(condition) must expand into code which, if condition is false,
causes (among other things) the line number to be printed out. The most
obvious way to do this is by use of __LINE__, though that's not required.
....
I presume that it refers to the fact that "%s" will copy an arbitrary
number of characters into the buffer pointed to by the corresponding
argument. If the number of characters exceeds the size of the buffer, you
get an overflow.
Note that %[ has the same issue.
Buffer overflow can be avoided by specification of a maximum field
width: "%32s". This is even mentioned in a footnote to the FAQ
<
http://www.c-faq.com/stdio/overflow.html>