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

Problem using sscanf...

Hi,

using sscanf, I'm trying to retrieve something, but nothing seems to
work.

Here's the pattern: SS%*s0%6s
Heres the data: SS000000395000000000DC-07998291174503725

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*s0" would work as if I was
using "%*21c0", but it doesnt.

Can someone tell me why ?

Regards,

AM.

Mar 21 '07 #1
5 3467
On Mar 21, 3:19 pm, "Alex Mathieu" <alex.math...@gmail.comwrote:
using sscanf, I'm trying to retrieve something, but nothing seems to
work.

Here's the pattern: SS%*s0%6s
Heres the data: SS000000395000000000DC-07998291174503725

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*s0" would work as if I was
using "%*21c0", but it doesnt.

Can someone tell me why ?
Nope, but comp.lang.c might help, or you might consider using strtok
or Boost.Tokenizer or even std::tr1::regex (aka Boost.Regex).

Cheers! --M

Mar 21 '07 #2
On Mar 21, 8:19 pm, "Alex Mathieu" <alex.math...@gmail.comwrote:
Hi,

using sscanf, I'm trying to retrieve something, but nothing seems to
work.

Here's the pattern: SS%*s0%6s
Heres the data: SS000000395000000000DC-07998291174503725

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*s0" would work as if I was
using "%*21c0", but it doesnt.

Can someone tell me why ?

Regards,

AM.
I think your problem is that "%*s" already reads the entire string,
because there is no seperator to stop parsing the string (a whitespace
for example). This means that after "%*s" there's nothing left to
parse.

You really should consider using a regular expressions library, as
mlimber already said.

Alex

Mar 21 '07 #3
Alex Mathieu wrote:
Hi,

using sscanf, I'm trying to retrieve something, but nothing seems to
work.

Here's the pattern: SS%*s0%6s
Heres the data: SS000000395000000000DC-07998291174503725

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*s0" would work as if I was
using "%*21c0", but it doesnt.

Can someone tell me why ?

Regards,

AM.
Because scanf is greedy. It will match till it cannot match anymore.
What your scan pattern does is read in the entire 'word' which means
that it will read till it hits a whitespace. Try /something like/ this:

"SS%*[^-]-0%[^]%*s"

What this says is to read in a string that consists of every character
that is not a '-', read in the -0 then read in a string that consists
of everything but a '', then read in the rest of the 'word' (this last
step not necessary if you are using sscanf).

Note: using scanf can be difficult to get right. I also find the return
value not very useful as it doesn't tell me where the parse completed in
case I want to continue from there, but it could be useful for other
purposes.

When I use scanf, I usually use a %n in the format, and limit the string
read in to stop buffer overflows like this:

int byteOffset = 0; // must init as sscanf will not change if doesn't
// reach %n.
char stuff[7];
stuff[sizeof(stuff)-1] = '\0'; // ensuring null termination of the
// string without initialising the rest
// of it.

// Note the "%6[^]", this keeps the stuff buffer from overflowing.
sscanf(buffer, "SS%*[^-]-0%6[^]%n", stuff, &byteOffset);
if (byteOffset != 0) {
printf("You have read in the string %s.\n", stuff);
}

However, if you change the size of stuff to contain less elements then
you must change this number too, this is a potential maintenance
problem. To getting around that I would do like so:

// not sure if there is a header that contains these two macros:
#define STRINGIZE2(x) #x
#define STRINGIZE(x) STRINGIZE2(x)

#define DIM 7
int byteOffset = 0; // must init as sscanf will not change if doesn't
// reach %n.
char stuff[DIM];
stuff[DIM-1] = '\0'; // ensuring null termination of the string,
// without initialising the rest of it.

// Note the "%" STRINGIZE(DIM) "[^]", this keeps the stuff buffer
// from overflowing, while allowing you to modify the dimension at
// a single point some time later.
sscanf(buffer, "SS%*[^-]-0%" STRINGIZE(DIM)"[^]%n", stuff
, &byteOffset);
if (byteOffset != 0) {
printf("You have read in the string %s.\n", stuff);
}
#undef DIM // remove extraneous macros from the global macro namespace
#undef STRINGIZE
#undef STRINGIZE2

Because of the difficulty in its use, many people choose not use it.
However, if used correctly, it can be very fast at parsing.

FYI, I wrote this without testing it. There may be errors in the code
posted.

Hope this helps.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
Mar 21 '07 #4
Yeah, seen this way this could be part of a solution and I REALLY
thank you..

Thing is that the sscanf is use into a log injector in our systems, so
I'm only specifying the pattern and the data to deal with... no very
much latitude. However, your solution with the "SS%*[^-]-0%[^]%*s"
pattern could help me for a while.

Actually, my problem is that I want to retrieve infos from a data
chunk where data are enclose between "" where "" is use as a
delimiter. Using regex it would be easy to retrieve the data with
something like *..., but with sscanf... this seems not too much
possible...

I'll try to think about a way to retrieve info easily from this
message...

However, thanks a lot for your very complete answer and the time you
took to wrote it down. It's not lost time, I'll try to implement that
solution on my own for test purpose :)

Regards,

Alexandre M.
Montral, Qubec

On 21 mar, 15:54, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
Alex Mathieuwrote:
Hi,
using sscanf, I'm trying to retrieve something, but nothing seems to
work.
Here's the pattern: SS%*s0%6s
Heres the data: SS000000395000000000DC-07998291174503725
Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*s0" would work as if I was
using "%*21c0", but it doesnt.
Can someone tell me why ?
Regards,
AM.

Because scanf is greedy. It will match till it cannot match anymore.
What your scan pattern does is read in the entire 'word' which means
that it will read till it hits a whitespace. Try /something like/ this:

"SS%*[^-]-0%[^]%*s"

What this says is to read in a string that consists of every character
that is not a '-', read in the -0 then read in a string that consists
of everything but a '', then read in the rest of the 'word' (this last
step not necessary if you are using sscanf).

Note: using scanf can be difficult to get right. I also find the return
value not very useful as it doesn't tell me where the parse completed in
case I want to continue from there, but it could be useful for other
purposes.

When I use scanf, I usually use a %n in the format, and limit the string
read in to stop buffer overflows like this:

int byteOffset = 0; // must init as sscanf will not change if doesn't
// reach %n.
char stuff[7];
stuff[sizeof(stuff)-1] = '\0'; // ensuring null termination of the
// string without initialising the rest
// of it.

// Note the "%6[^]", this keeps the stuff buffer from overflowing.
sscanf(buffer, "SS%*[^-]-0%6[^]%n", stuff, &byteOffset);
if (byteOffset != 0) {
printf("You have read in the string %s.\n", stuff);
}

However, if you change the size of stuff to contain less elements then
you must change this number too, this is a potential maintenance
problem. To getting around that I would do like so:

// not sure if there is a header that contains these two macros:
#define STRINGIZE2(x) #x
#define STRINGIZE(x) STRINGIZE2(x)

#define DIM 7
int byteOffset = 0; // must init as sscanf will not change if doesn't
// reach %n.
char stuff[DIM];
stuff[DIM-1] = '\0'; // ensuring null termination of the string,
// without initialising the rest of it.

// Note the "%" STRINGIZE(DIM) "[^]", this keeps the stuff buffer
// from overflowing, while allowing you to modify the dimension at
// a single point some time later.
sscanf(buffer, "SS%*[^-]-0%" STRINGIZE(DIM)"[^]%n", stuff
, &byteOffset);
if (byteOffset != 0) {
printf("You have read in the string %s.\n", stuff);
}
#undef DIM // remove extraneous macros from the global macro namespace
#undef STRINGIZE
#undef STRINGIZE2

Because of the difficulty in its use, many people choose not use it.
However, if used correctly, it can be very fast at parsing.

FYI, I wrote this without testing it. There may be errors in the code
posted.

Hope this helps.

Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/

Mar 22 '07 #5
Alex Mathieu wrote:
Yeah, seen this way this could be part of a solution and I REALLY
thank you..

Thing is that the sscanf is use into a log injector in our systems, so
I'm only specifying the pattern and the data to deal with... no very
much latitude. However, your solution with the "SS%*[^-]-0%[^]%*s"
pattern could help me for a while.

Actually, my problem is that I want to retrieve infos from a data
chunk where data are enclose between "" where "" is use as a
delimiter. Using regex it would be easy to retrieve the data with
something like *..., but with sscanf... this seems not too much
possible...

I'll try to think about a way to retrieve info easily from this
message...

However, thanks a lot for your very complete answer and the time you
took to wrote it down. It's not lost time, I'll try to implement that
solution on my own for test purpose :)
No problem Alex, but please don't top post or quote unnecessarily, it is
considered rude on the newsgroups.

Re your more general problem of getting data between the delimiters.
Use something like this:

// x is the max number of chars to read in.
#define RECORD(x) "%" #x "[^]"
#define RECORD_IGNORE "%*[^]"

char str[8] = {}; // init entire array to '\0'. Slightly more
// overhead than just init the last one to '\0' but
// for small non-time-critical applications, it
// should be fine.
sscanf(buffer, RECORD_IGNORE RECORD(7) RECORD_IGNORE, str);

- or -

#define LEN 7
char str[LEN+1] = {};
sscanf(buffer, RECORD_IGNORE RECORD(LEN) RECORD_IGNORE, str);

- *don't do* -

#define DIM 8
char str[DIM] = {};
sscanf(buffer, RECORD_IGNORE RECORD(DIM-1) RECORD_IGNORE, str);

as your string will become "%*[^]%8-1[^]%*[^]" which doesn't make
sense.

Good luck.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 22 '07 #6

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

Similar topics

1
by: PX | last post by:
Greetings, Say I want to read a file line by line so I used fgets(). And I want to parse strings from each line of the file, I used sscanf(). But there's a strange problem that each time I run...
4
by: Ivan Lam | last post by:
Hi All, Thanks for reading my post! I have a problem that using the scanf function. I would like to scan a value from a line like: file:c:\program files\mpd\mpd.exe however, when I read...
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;
4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
4
by: lynx_ace | last post by:
Hi everyone. I need a little bit help here...I have an assignment and it is working fine until the last part which I can't solve. So here's the code in simple form #define maxlength 200 ...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
9
by: Tim Streater | last post by:
I'm obviously missing something here. I have a line like this (in $line), and I want all the text within the double quotes:: somestr="string I want" with some whitespace at the beginning. So I...
5
by: Timo | last post by:
I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic: I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49"....
4
by: utab | last post by:
Dear all, I have to interface some C code in C++, but I had a problem with sscanf function, it has been some time I have not used C and I could not figure out my problem. Simple code is below, I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.