sscanf return value

G

gio

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
 
A

Army1987

"gio" <[email protected]> ha scritto nel messaggio
[snip]
fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'

ret=sscanf(str1, "%s", str2); [snip]
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).
 
C

CBFalconer

gio said:
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.
 
M

Malcolm McLean

gio said:
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.
 
G

gio

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
 
K

Keith Thompson

Army1987 said:
"gio" <[email protected]> ha scritto nel messaggio
[snip]
fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'

ret=sscanf(str1, "%s", str2); [snip]
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().
 
K

Keith Thompson

gio said:
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.
 
C

Chris Torek

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top