473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

sscanf return value

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

Apr 21 '07 #1
7 11602
"gio" <gi**************@yahoo.itha scritto nel messaggio
news:11*********************@b75g2000hsg.googlegro ups.com...
[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).
Apr 21 '07 #2
gio wrote:
>
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

Apr 21 '07 #3

"gio" <gi**************@yahoo.itwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
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

Apr 21 '07 #4
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
Apr 21 '07 #5
"Army1987" <pl********@for.itwrites:
"gio" <gi**************@yahoo.itha scritto nel messaggio
news:11*********************@b75g2000hsg.googlegro ups.com...
[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().

--
Keith Thompson (The_Other_Keith) ks***@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"
Apr 21 '07 #6
gio <gi**************@yahoo.itwrites:
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) ks***@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"
Apr 21 '07 #7
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.
Apr 22 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
5
by: jchludzinski | last post by:
I'm using strtok() to parse thru a line and read different numbers: float value; char *token; token = strtok( line, " " ); .... sscanf( token, "%f", &value ); These results are less...
10
by: broeisi | last post by:
What advantages does sscanf offer over scanf? I had the following code: #include <stdio.h> #include <string.h> int main(void) { int start, finish, values;
9
by: GrispernMix | last post by:
bool variant_t::Convert( fieldtype_t newType ) { if ( newType == fieldType ) { return true; } // // Converting to a null value is easy. //
8
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
9
by: Bint | last post by:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc i can use sscanf(string,"success=%d", &d) to get the success value. but after that i just want to read name and u pairs...
16
by: Chad | last post by:
Given the following #include <stdio.h> int main(void) { char line; long arg1, arg2; while(fgets(line, BUFSIZ, stdin) != NULL){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.