473,411 Members | 1,982 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,411 software developers and data experts.

Parsing a simple string

Hello,

I am trying to parse a string and I am surprised by the result(*)
When using:
sscanf(s, "%03u%c", &val, &f);
If a number less than 3 digits is found, shouldn't sscanf report
that ?

Thanks
Mathieu
(*)
#include <stdio.h>

void parse(const char *s)
{
unsigned int val;
char f;
int k = sscanf(s, "%03u%c", &val, &f);
printf( "%d %u %c\n", k, val, f);
}

int main()
{
// 21 years:
parse( "021Y" );
// -1 year (invalid)
parse( "0-1Y" );

return 0;
}

May 10 '06 #1
19 3423
mathieu wrote:
Hello,

I am trying to parse a string and I am surprised by the result(*)
When using:
sscanf(s, "%03u%c", &val, &f);
If a number less than 3 digits is found, shouldn't sscanf report
that ?


scanf conversion specifiers are not the same as printf's. The %03u
obviously doesn't do what you think it does. The 0 is superfluous and
the 3 just indicates that the *maximum* number of characters to be read
(after skipping any leading whitespace) for that conversion is 3.
Check your documentation for details.

Robert Gamble

May 10 '06 #2
Oooops I missed the 'maximum'. Thanks for the info.
I guess I simply make sure that all of the three first char are
decimal.

Mathieu

May 10 '06 #3
Just for ref. Here is the final solution:

void parse(const char *s)
{
unsigned int val;
char f;
if( !isdigit(s[0])
|| !isdigit(s[1])
|| !isdigit(s[2]))
{
printf( "error\n");
}
else
{
int k = sscanf(s, "%03u%c", &val, &f);
printf( "%d %u %c\n", k, val, f);
}
}

May 10 '06 #4


mathieu wrote On 05/09/06 16:03,:
Just for ref. Here is the final solution:

void parse(const char *s)
{
unsigned int val;
char f;
if( !isdigit(s[0])
|| !isdigit(s[1])
|| !isdigit(s[2]))
{
printf( "error\n");
}
else
{
int k = sscanf(s, "%03u%c", &val, &f);
printf( "%d %u %c\n", k, val, f);
}
}


Just for reference, the above code still has an
error that can cause trouble on some systems. The
problem is that `char' can be a signed type or an
unsigned type, whichever the implementation finds
more convenient. On systems where `char' is signed,
it could happen that any or all of s[0] through s[2]
might have negative values. However, isdigit() only
works on non-negative `char' values and on the negative
value EOF; feed it some other negative value, and
unpredictable things will happen. (True, the digits
are known to have positive values -- but if you knew in
advance that you were dealing with digits, you wouldn't
need the isdigit() calls in the first place!)

Fortunately, the cure (for isdigit() and for the
rest of the <ctype.h> functions) is simple: When you
apply one of them to a `char' value, just convert the
`char' to the corresponding non-negative value:

if ( !isdigit( (unsigned char)s[0] ) ...

Note the part about applying the functions to a
`char'. If you apply them to the `int' value returned
by getchar() or getc() or fgetc(), you should not convert
as shown: the value is already either a converted non-
negative character or else it is the negative number EOF.
If it is EOF, you don't want to convert it from negative
to positive; you want to leave it alone. And if it's not
EOF, it's already non-negative and needs no conversion.
But when you're plucking character values from a string
or something, you need the sign conversion.

--
Er*********@sun.com

May 10 '06 #5
mathieu wrote:
Just for ref. Here is the final solution:

void parse(const char *s)
{
unsigned int val;
char f;
if( !isdigit(s[0])
|| !isdigit(s[1])
|| !isdigit(s[2]))
{
printf( "error\n");
}
else
{
int k = sscanf(s, "%03u%c", &val, &f);
printf( "%d %u %c\n", k, val, f);
}
}


With a sscanf format of "%u", leading white space in the input
string, if present, is skipped during conversion. You may want to
consider the possibility of white space being in your input string.
In such a case, a function like this may be useful:

#include <string.h>

size_t count_digits_after_ws (const char *s)
{
const char *ptr = s;
size_t offset = strspn(ptr, " \n\t\r\f\v");
ptr += offset;
offset = strspn(ptr, "0123456789");
return offset;
}

--
Hope this helps,
Steven

May 10 '06 #6
In this case I am better of with something like:

if( '0' > s[0] || s[0] > '9' )

May 10 '06 #7
mathieu wrote:
In this case I am better of with something like:

if( '0' > s[0] || s[0] > '9' )

Check line 42. There is an error there for sure.
May 10 '06 #8
I am saying that:

if( s[0] < '0' || s[0] > '9'
|| s[1] < '0' || s[1] > '9'
|| s[2] < '0' || s[2] > '9' )
{

is equivalent to my previous post with the call of isdigit. But as far
as understand it should not suffer from the unsigned char cast, right ?

May 10 '06 #9
"mathieu" <ma***************@gmail.com> writes:
I am saying that:

if( s[0] < '0' || s[0] > '9'
|| s[1] < '0' || s[1] > '9'
|| s[2] < '0' || s[2] > '9' )
{

is equivalent to my previous post with the call of isdigit. But as far
as understand it should not suffer from the unsigned char cast, right ?


I have no idea, since you didn't include any information from your
previous post.

Read <http://cfaj.freeshell.org/google/>.

--
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.
May 10 '06 #10
mathieu wrote:

I am saying that:

if( s[0] < '0' || s[0] > '9'
|| s[1] < '0' || s[1] > '9'
|| s[2] < '0' || s[2] > '9' )
{

is equivalent to my previous post with the call of isdigit. But as far
as understand it should not suffer from the unsigned char cast,
right ?


Right.

--
pete
May 10 '06 #11
"mathieu" <ma***************@gmail.com> brokenly-Google-Grouped:
I am saying that:

if( s[0] < '0' || s[0] > '9'
|| s[1] < '0' || s[1] > '9'
|| s[2] < '0' || s[2] > '9' )
{

is equivalent to my previous post with the call of isdigit. But as far
as understand it should not suffer from the unsigned char cast, right ?


There is no unsigned char cast in that code, so no, it doesn't suffer
from any unsigned char casts.

Richard
May 11 '06 #12
Sorry for that. I'll keep that in mind. I did vote for the feature
request at:

http://groups-beta.google.com/suppor..._type=features

Thx
-M

May 11 '06 #13
mathieu wrote:
Sorry for that.
Sorry for what? Provide context please when quoting. There are links at
http://clc-wiki.net/wiki/Intro_to_clc to sites with instructions.
I'll keep that in mind. I did vote for the feature
request at:

http://groups-beta.google.com/suppor..._type=features


Vote for which feature? Google being separated from Usenet so we don't
get all these context-less posts?
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 11 '06 #14
> Vote for which feature? Google being separated from Usenet so we don't
get all these context-less posts?


Ok I said, I apologized.
What do you do when you only have read access to the newsgroups,
google.groups is very convenient solution, not perfect yet, that's all.

May 11 '06 #15
"mathieu" <ma***************@gmail.com> writes:
Sorry for that. I'll keep that in mind. I did vote for the feature
request at:

http://groups-beta.google.com/suppor..._type=features


I *think* you're saying you voted for the "Default quoting of previous
message in replies" feature. Thanks for that, but it's ironic that
you mention this while not using the simple workaround that lets you
do this in spite of Google's broken interface. (And in a later
followup in this thread, you quote a previous article but snip the
attribution line.)

You *have* read <http://cfaj.freeshell.org/google/>, right?

--
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.
May 11 '06 #16
And incessant whining about Google has what to do with
the programming language C?
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
# mathieu wrote:
# > Sorry for that.
#
# Sorry for what? Provide context please when quoting. There are links at
# http://clc-wiki.net/wiki/Intro_to_clc to sites with instructions.
#
# > I'll keep that in mind. I did vote for the feature
# > request at:
# >
# > http://groups-beta.google.com/suppor..._type=features
#
# Vote for which feature? Google being separated from Usenet so we don't
# get all these context-less posts?
# --
# Flash Gordon, living in interesting times.
# Web site - http://home.flash-gordon.me.uk/
# comp.lang.c posting guidelines and intro:
# http://clc-wiki.net/wiki/Intro_to_clc
#
# Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
#
#

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Mention something out of a Charleton Heston movie, and suddenly
everybody's a theology scholar.
May 13 '06 #17
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
And incessant whining about Google has what to do with
the programming language C?


It makes it possible to discuss it coherently.

I see you've decided to start top-posting. I can't say I'm surprised.

--
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.
May 13 '06 #18
Keith Thompson wrote:
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> writes:
And incessant whining about Google has what to do with
the programming language C?


It makes it possible to discuss it coherently.

I see you've decided to start top-posting. I can't say I'm surprised.


Trolls tend to degenerate as their current bag o' tricks gets stale. I
killfiled him long ago over past antics.

Brian
May 13 '06 #19

Keith Thompson wrote:
I *think* you're saying you voted for the "Default quoting of previous
message in replies" feature. Thanks for that, but it's ironic that
you mention this while not using the simple workaround that lets you
do this in spite of Google's broken interface. (And in a later
followup in this thread, you quote a previous article but snip the
attribution line.)


I'll make sure to keep attribution line, too. Thx

-M

May 17 '06 #20

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

Similar topics

51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
20
by: Chris LaJoie | last post by:
I'm looking for some kind of simple string compression code I can use. I'm not looking for SharpZipLib. Their implimentation spans several classes and is very complex. I'm just looking for...
0
by: Robert Ludig | last post by:
How do I bind a textbox to a simple string varaible with databinding? I managed to do the binding but unfortnatedly the textvox does not get updated when I change the string wich the textbox is...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
50
by: Doug | last post by:
I can't imagine this is that hard but I'm sure having a struggle finding it. How do I convert a value like "111403" do a DateTime variable in DotNet (i.e. 11/14/2003)?
10
by: Stu | last post by:
Can somebody please tell me the most effient away to parse the date YYYYMMDD from the following string. char *date_path = "/dira/dirb/dirc/dird/2006/12/04" Note: the number of directories...
5
by: baju123 | last post by:
Hi, Would anybody tell me an efficient method to parse a string inside a variable which is line separated ... Like this value1: line1 /n value2: line2 /n value3: line3 /n
8
by: thunderbolt | last post by:
Hi, How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005 (Dec)" in C#? The number within the brackets represent month.. am new to c# and breaking head for couple of hours over...
6
by: vedrandekovic | last post by:
Hello, I have one simple string, backspace character question.Here is my example: "HelloBSworld" Should this character "\b" (backspace) in this text return this: "Helloworld"?
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
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...

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.