473,321 Members | 1,748 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,321 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%*sþ0þ%6s
Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ

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

Can someone tell me why ?

Regards,

AM.

Mar 21 '07 #1
5 3475
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%*sþ0þ%6s
Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*sþ0þ" would work as if I was
using "%*21cþ0þ", 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%*sþ0þ%6s
Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*sþ0þ" would work as if I was
using "%*21cþ0þ", 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%*sþ0þ%6s
Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ

Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*sþ0þ" would work as if I was
using "%*21cþ0þ", 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.
Montréal, Québec

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%*sþ0þ%6s
Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ
Actually, I would like to retrieve the "799829" from the data, but it
always failed. I thought that the "%*sþ0þ" would work as if I was
using "%*21cþ0þ", 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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.