In article <11*********************@b75g2000hsg.googlegroups. com>
gio <gi**************@yahoo.itwrote:
>The sscanf man page specifies that "...this function return EOF if the
end of input is reached before the first conversion. EOF is also
returned if a read error occur, in which case the error indicator of
the stream is set(see ferror(3))...".
The scanf(), fscanf(), and sscanf() functions (and in C99, the
v* variants) generally all use a single "scanf engine". This
engine does indeed return EOF if input ends before the first
conversion, and does indeed return EOF if an input error occurs.
However, when scanning a string with sscanf(), read errors
simply never occur, because the string is (by the definition of
"a string") already entirely in memory and entirely accessible.
(Remember, the definition of a "string" in C is "a data structure
consisting of one or more "char"s in sequence, terminated by the
first '\0' character. So if buf[] has size 99 and its first three
bytes are 0, 42, and 0 respectively, buf[] contains a string that
is zero characters long, occupying one byte. Change buf[0] from
0 to 70 and buf[] now contains a string that is two characters
long, occupying three bytes. If strlen(str) is K, the string data
structure uses K+1 bytes. Here the buffer has room for 99 bytes,
so can contain strings up to 98 characters long.
If you call sscanf() on some data structure that is not "a string",
the call itself is in error, and all bets are invalidated long
before anything could cause a "read error". Strings are therefore
inherently different from stdio streams, where errors can occur
when calling getc() or fgetc() or its equivalent.)
>If ret = EOF after sscanf I want know if the end of input is reached
or if a read error occur, but I don't understand how I must call
ferror (int ferror(FILE *stream)), because I don't read from a file.
Indeed, since the stdio stream involved is invisible (either does
not exist, or has been created but then destroyed between your call
to sscanf() and its returns) there is no stream to query -- but
since a read error cannot have occurred (if you gave valid arguments
to sscanf() anyway), the only way to get EOF back is to have reached
the end of the string before the first conversion.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.