Connecting Tech Pros Worldwide Forums | Help | Site Map

sscanf return value

gio
Guest
 
Posts: n/a
#1: Apr 21 '07
suppose I have:
....
char str1[LEN1];
char str2[LEN2];
int ret;

fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'

ret=sscanf(str1, "%s", str2);
....


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))...".

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.
Someone can help me please?
thanks


Army1987
Guest
 
Posts: n/a
#2: Apr 21 '07

re: sscanf return value


"gio" <giorginooo.rossi@yahoo.itha scritto nel messaggio
news:1177160264.866887.92950@b75g2000hsg.googlegro ups.com...
[snip]
Quote:
fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'
>
ret=sscanf(str1, "%s", str2);
[snip]
Quote:
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.
Use ferror(stdin).


CBFalconer
Guest
 
Posts: n/a
#3: Apr 21 '07

re: sscanf return value


gio wrote:
Quote:
>
suppose I have:
...
char str1[LEN1];
char str2[LEN2];
int ret;
>
fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'
>
ret=sscanf(str1, "%s", str2);
...
Things are undefined, because str2 is not initialized. str1 may
not be initialized, if fgets fails.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>



--
Posted via a free Usenet account from http://www.teranews.com

Malcolm McLean
Guest
 
Posts: n/a
#4: Apr 21 '07

re: sscanf return value



"gio" <giorginooo.rossi@yahoo.itwrote in message
news:1177160264.866887.92950@b75g2000hsg.googlegro ups.com...
Quote:
suppose I have:
...
char str1[LEN1];
char str2[LEN2];
int ret;
>
fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'
>
ret=sscanf(str1, "%s", str2);
...
>
>
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))...".
>
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.
Someone can help me please?
thanks
>
sscanf() isn't an IO function and it has no business setting file errors. I
suspect a garbled man page confusing it with fscanf(). It returns the
number of fields successfully converted or EOF on end of string. Normally

if(sscanf(str, "%d %d %d ", &x, &y, &z) != 3)
/* scan error */ exit(EXIT_FAILURE);

is the idiom to use. Occasionally you might need to detect an end of string
condition and check for EOF.

fgets() will set ferror() on an IO error.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

gio
Guest
 
Posts: n/a
#5: Apr 21 '07

re: sscanf return value


thanks all but this answers not focus on the problem.
1. I check for fgets error so the result cannot be undefined (and
however this is not what I asked)
2. I cannot write somethings like:

if(sscanf(str1, "%s", str2) != 1)
/* scan error */ exit(EXIT_FAILURE);

This code will exit also if str1, after fgets, just contains '\n'
'\0' (this is tha case I type from input RETURN), because sscanf
return EOF, and in this case I should not exit


Keith Thompson
Guest
 
Posts: n/a
#6: Apr 21 '07

re: sscanf return value


"Army1987" <please.ask@for.itwrites:
Quote:
"gio" <giorginooo.rossi@yahoo.itha scritto nel messaggio
news:1177160264.866887.92950@b75g2000hsg.googlegro ups.com...
[snip]
Quote:
>fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'
>>
>ret=sscanf(str1, "%s", str2);
[snip]
Quote:
>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.
>
Use ferror(stdin).
sscanf() reads from a string, not from stdin; if it sets stdin's error
flag, it's badly broken. You're probably thinking of scanf().

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Keith Thompson
Guest
 
Posts: n/a
#7: Apr 21 '07

re: sscanf return value


gio <giorginooo.rossi@yahoo.itwrites:
Quote:
thanks all but this answers not focus on the problem.
1. I check for fgets error so the result cannot be undefined (and
however this is not what I asked)
2. I cannot write somethings like:
>
if(sscanf(str1, "%s", str2) != 1)
/* scan error */ exit(EXIT_FAILURE);
>
This code will exit also if str1, after fgets, just contains '\n'
'\0' (this is tha case I type from input RETURN), because sscanf
return EOF, and in this case I should not exit
If you shouldn't exit, don't exit.

I think you're trying to distinguish between two different kinds of
errors, one fatal and one non-fatal. If str1 is just "\n", you want
that to be a non-fatal error. Can you give an example of what should
be considered a fatal error?

Note that, since sscanf() doesn't perform input, it can't suffer from
an input error.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Chris Torek
Guest
 
Posts: n/a
#8: Apr 22 '07

re: sscanf return value


In article <1177160264.866887.92950@b75g2000hsg.googlegroups. com>
gio <giorginooo.rossi@yahoo.itwrote:
Quote:
>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.)
Quote:
>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.
Closed Thread