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/]______\/