473,779 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11629
"gio" <gi************ **@yahoo.itha scritto nel messaggio
news:11******** *************@b 75g2000hsg.goog legroups.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.securityfoc us.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.itwrot e in message
news:11******** *************@b 75g2000hsg.goog legroups.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_FAILU RE);

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_FAILU RE);

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******** *************@b 75g2000hsg.goog legroups.com...
[snip]
>fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0'

ret=sscanf(str 1, "%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_Keit h) 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.itwrit es:
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_FAILU RE);

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_Keit h) 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************ *********@b75g2 000hsg.googlegr oups.com>
gio <gi************ **@yahoo.itwrot e:
>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
4259
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
5473
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
6405
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 precise than I had expected:
10
2986
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
2980
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
2389
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 text string, another is a decimal, next one is float, etc.). I have GCC 4.0.1 on Mac OS X Tiger. Here is an example of what I am trying to do.
24
2974
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 modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
9
6649
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 until there are no more. if Iwere to do a sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of the first u/name, right? is there any way to retrieve the string pointer position from sscanf so that I can just call it again...
16
2907
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
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.