sscanf to parse?

B

Bill Cunningham

How would you use sscanf to parse? What comes to mind for me would be
some std functions from. ctype.h string.h and stdlib.h like strtol.
 
O

osmium

Bill Cunningham said:
How would you use sscanf to parse? What comes to mind for me would be
some std functions from. ctype.h string.h and stdlib.h like strtol.

Rather than using impressive sounding words such as "parse" it would be more
helpful if you would explain what it is you want to do. Do you want to
write a C compiler? Something less than that?

For starters, sscanf can only distinguish between a few kinds of numbers and
a few other things; not a good base for anything I would call "parse"
without a lot of hemming and hawing.
 
B

Bill Cunningham

osmium said:
Rather than using impressive sounding words such as "parse" it would
be more helpful if you would explain what it is you want to do. Do
you want to write a C compiler?

LOL I think we know better.

Something less than that?
For starters, sscanf can only distinguish between a few kinds of
numbers and a few other things; not a good base for anything I would
call "parse" without a lot of hemming and hawing.

I heard someone else say they used it to "parse". I wasn't sure what
they meant either. Let me ask a more reasonable question: Why would sscanf
be needed for? What can you do with it? Or do people mainly do with it. For
input conversion maybe scanf would be better. When I want input I use this.

char buf[20];
fgets(buf,sizeof buf,stdin);

But that's like using puts for stdout. There's no conversion specification.
 
B

Barry Schwarz

How would you use sscanf to parse? What comes to mind for me would be
some std functions from. ctype.h string.h and stdlib.h like strtol.

If you want to use sscanf, why are you talking about strtol? If you
want to use strtol, why are you talking about sscanf? Can you give an
example of where you think you would need to use both to extract a
single integer value from a string?

Highly general questions result in highly general responses. Here is
one that will work no matter what you want:

In a loop, call sscanf with a %c format to extract each character
from the string in turn.

As each character is extracted, append it to a buffer.

Repeat until you are certain that you have extracted all the
characters you need (i.e, the "token" is complete).

Process the buffer as required by the super-secret requirements
you aren't willing to discuss with us.
 
O

osmium

:

I heard someone else say they used it to "parse". I wasn't sure what
they meant either. Let me ask a more reasonable question: Why would sscanf
be needed for? What can you do with it? Or do people mainly do with it.
For input conversion maybe scanf would be better.

As has been said, sscanf gives you additional "shots" at the data, whereas
with scanf you only get one attempt. I have difficulty coming up with
examples as to why one would want to do that. There is a pretty good set of
conversion functions in <stdlib.h> that do things very similar to the things
in sscanf. I could see a beginner who hadn't yet learned of what is in
<stdlib.h> using sscanf instead, since almost everyone has to learn scanf
early on. Even if it is to learn "don't use scanf."

The libraries are kind of like hidden jewels, you wander around and all of a
sudden turn up this wonderful little thing you can do. I think very few
instructors devote much attention to covering *all* the function libraries.
And there is a lot of self teaching in computer languages as well.

So ... try to engage the attention of the guy who wrote the thing that
triggered your attention in the first place.
 
J

James Kuyper

:



As has been said, sscanf gives you additional "shots" at the data, whereas
with scanf you only get one attempt. I have difficulty coming up with
examples as to why one would want to do that. There is a pretty good set of
conversion functions in <stdlib.h> that do things very similar to the things
in sscanf. I could see a beginner who hadn't yet learned of what is in
<stdlib.h> using sscanf instead, since almost everyone has to learn scanf
early on. Even if it is to learn "don't use scanf."

The libraries are kind of like hidden jewels, you wander around and all of a
sudden turn up this wonderful little thing you can do. I think very few
instructors devote much attention to covering *all* the function libraries.
And there is a lot of self teaching in computer languages as well.

So ... try to engage the attention of the guy who wrote the thing that
triggered your attention in the first place.

That might be difficult for Bill to do directly. The two most recent
previous occurrences of "parse" referring to sscanf() in this newsgroup
were committed by me, in the thread "Reading a data file", and I've got
Bill killfiled. On the other hand, the third most recent was by Nobody,
and that was in the thread "scanf and sscanf" which was started by Bill
himself, so it might not have been me he was referring to.

sscanf(), like scanf(), parses the characters passed to it in accordance
with the specified format. As long as you're reading text data whose
format can be easily and accurately described using scanf() format
strings, it's a fairly good way of doing so, except for it's poor
handling of out-of-range numerical data. However, I didn't claim that
it's a good tool for general-purpose parsing tasks. I'd use yacc and lex
(or bison and flex) for anything seriously complicated, such as
correctly parsing C code.
 
J

James Kuyper

:



As has been said, sscanf gives you additional "shots" at the data, whereas
with scanf you only get one attempt. I have difficulty coming up with
examples as to why one would want to do that. There is a pretty good set of
conversion functions in <stdlib.h> that do things very similar to the things
in sscanf. I could see a beginner who hadn't yet learned of what is in
<stdlib.h> using sscanf instead, since almost everyone has to learn scanf
early on. Even if it is to learn "don't use scanf."

The libraries are kind of like hidden jewels, you wander around and all of a
sudden turn up this wonderful little thing you can do. I think very few
instructors devote much attention to covering *all* the function libraries.
And there is a lot of self teaching in computer languages as well.

So ... try to engage the attention of the guy who wrote the thing that
triggered your attention in the first place.

That might be difficult for Bill to do directly. The two most recent
previous occurrences of "parse" referring to sscanf() in this newsgroup
were committed by me, in the thread "Reading a data file", and I've got
Bill killfiled. On the other hand, the third most recent was by Nobody,
and that was in the thread "scanf and sscanf" which was started by Bill
himself, so it might not have been me he was referring to.

sscanf(), like scanf(), parses the characters passed to it in accordance
with the specified format. As long as you're reading text data whose
format can be easily and accurately described using scanf() format
strings, it's a fairly good way of doing so, except for it's poor
handling of out-of-range numerical data. However, I didn't claim that
it's a good tool for general-purpose parsing tasks. I'd use yacc and lex
(or bison and flex) for anything seriously complicated, such as
correctly parsing C code.
 
O

osmium

James Kuyper said:
That might be difficult for Bill to do directly. The two most recent
previous occurrences of "parse" referring to sscanf() in this newsgroup
were committed by me, in the thread "Reading a data file", and I've got
Bill killfiled. On the other hand, the third most recent was by Nobody,
and that was in the thread "scanf and sscanf" which was started by Bill
himself, so it might not have been me he was referring to.

sscanf(), like scanf(), parses the characters passed to it in accordance
with the specified format. As long as you're reading text data whose
format can be easily and accurately described using scanf() format
strings, it's a fairly good way of doing so, except for it's poor
handling of out-of-range numerical data. However, I didn't claim that
it's a good tool for general-purpose parsing tasks. I'd use yacc and lex
(or bison and flex) for anything seriously complicated, such as
correctly parsing C code.

There you go, Bill, it's not a real good idea. Now please write this down
somewhere so that you can find it before posting the same question again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top