EmbeddedRelated.com
Forums
Memfault Beyond the Launch

question on sscanf

Started by Miles Gazic September 10, 2004
I am using CrossWorks, and can't get sscanf to work. It returns a value
that indicates that the parameters were scanned, but the values it puts
in them appear random. For any given string it comes up with the same
values, but they don't appear to be related to the string

I've seen this happen before and just used strtol instead, as a work
around. Doesn't look like there is an atof or strtod available, so I'd
really rather use a sscanf with %f, especially since there are several
variable length fields with delimiters.

Any 'gotchas' I need to be aware of when using sscanf on CrossStudio
with the MSP430?

Thanks,
Miles

PS: I apologize if I already asked this question. I searched the
archives and my sent mail, and don't see mention of it, but I know I
asked *someone* at some time, just don't remember who or when. :D

Beginning Microcontrollers with the MSP430

> I am using CrossWorks, and can't get sscanf to work.

The IAR compiler (some non-current version) doesn't support sscanf()
with %f either. It was a big letdown when I learned that the hard
way.

There must be some magical reason why the compilers don't support
it. Like you, I settled for manual string parsing and a call to atof
() but apparently you don't even have atof(). Might be time for a
compiler change. :(

JJS
I use sscanf in my IAR compiled programs without a problem.

Doesn't sscanf return the number of params successfully scanned, not the
length of the buffer?
Miles,

The return value of scanf is the number of items successfully scanned.
You need to set up the IDE to link in the correct version of scanf()
i.e. enable floating point I/O in the project, otherwise %f won't work.

I seem to remember a problem in the scanf code at one point which caused
the problem you see. This is now fixed, and we'll be releasing a new
version of the IDE just after I've written the last pieces of
documentation.

-- Paul.
John,

> > I am using CrossWorks, and can't get sscanf to work.
>
> The IAR compiler (some non-current version) doesn't support
> sscanf() with %f either. It was a big letdown when I learned
> that the hard way.

You need to link the version of scanf in that supports floating point...

> There must be some magical reason why the compilers don't
> support it.

Correct. You might just want to scan integers, never need %f, and want
to fit it into a small device. For this, you want an integer-only
version of scanf. If you need the full-blown thing, then link in the
full-blown scanf including character class support, long lomg, floating
point, the works.

> Like you, I settled for manual string parsing
> and a call to atof
> () but apparently you don't even have atof(). Might be time
> for a compiler change. :(

Atof is now part of the CrossWorks library.

-- Paul.
To use sscanf in the IAR compiler you must change the configuration the the XLink control file from the default of:

-e_medium_read=_formatted_read

The default allows sscanf but does not allow floating point numbers.

To use the full ANSI version allowing floating point number remove the line in your .xcl file.

Ted
Paul,

Thanks for the response. Do I need an updated IDE to get the fix for
scanf, or to get atof? or do I just need to replace a library? If it's
coming out soon I'll wait, if it's a couple weeks off I'll throw
something together to do without it.

Thanks,
Miles

Memfault Beyond the Launch