public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: sscanf trouble
@ 2011-01-25 19:57 Jonathan Andrews
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Andrews @ 2011-01-25 19:57 UTC (permalink / raw)
  To: gcc-help

n Tue, 2011-01-25 at 11:18 -0700, Bill McEnaney wrote:
> Why not replace the "=" with another character before you call sscanf?
> 
> Cheers,
> Bill
> Jon wrote:
> 
> >

Thanks for the idea, this was finger trouble by myself.

I was corrupting one of my variables between the call to sscanf and the
first printf.

Cheers,
Jon


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sscanf trouble
@ 2011-01-25 18:18 Bill McEnaney
  0 siblings, 0 replies; 5+ messages in thread
From: Bill McEnaney @ 2011-01-25 18:18 UTC (permalink / raw)
  To: jon, gcc-help

Why not replace the "=" with another character before you call sscanf?

Cheers,
Bill
Jon wrote:

> Hi people.
> 
> Can anyone help with what should be a simple problem. I have some text
> in the following form. Its a temperature reading from a wireless sensor,
> the format is T<sensor number>=<float ish> <TAB><TAB><Two digit ascii
> checksum>
> 
> T1=18.0<TAB><TAB>XX\n\r
> 
> Some examples:
> 
>         T1=-11.5        EA
>         T1=24.0         9D
> 
> Im trying to convert this string into a sensor ID, a floating point
> reading and a checksum as 3 variables using sscanf
> 
> sscanf(line,"T%d=%f\t\t%X",&sensorid,&temperature,&checksum);
> 
> No amount of variations on a theme seem to give me an entire decode
> here, seems the equals seems to screw things up.  Anyone any ideas how I
> can make this work?
> 
> Thanks for any advice,
> Jon
> 
> 
> 
> 
> 

________________________________________________________________
Please visit a saintly hero:
http://www.jakemoore.org

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sscanf trouble
  2011-01-25 11:08 Jonathan Andrews
  2011-01-25 12:18 ` Jonathan Wakely
@ 2011-01-25 12:21 ` Adam Stein
  1 sibling, 0 replies; 5+ messages in thread
From: Adam Stein @ 2011-01-25 12:21 UTC (permalink / raw)
  To: jon; +Cc: gcc-help

On Tue, 2011-01-25 at 11:09 +0000, Jonathan Andrews wrote:
> Hi people.
> 
> Can anyone help with what should be a simple problem. I have some text
> in the following form. Its a temperature reading from a wireless sensor,
> the format is T<sensor number>=<float ish> <TAB><TAB><Two digit ascii
> checksum>
> 
> T1=18.0<TAB><TAB>XX\n\r
> 
> Some examples:
> 
>         T1=-11.5        EA
>         T1=24.0         9D
> 
> Im trying to convert this string into a sensor ID, a floating point
> reading and a checksum as 3 variables using sscanf
> 
> sscanf(line,"T%d=%f\t\t%X",&sensorid,&temperature,&checksum);
> 
> No amount of variations on a theme seem to give me an entire decode
> here, seems the equals seems to screw things up.  Anyone any ideas how I
> can make this work?
> 
> Thanks for any advice,
> Jon

Compiled the following using gcc v4.4.5 on a Fedora 13 system and it
seemed to work fine:

#include <stdio.h>

main()
{
    int sensorid, checksum;
    float temperature;
    char *line1 = "T1=-11.5\t\tEA\n\r";
    char *line2 = "T1=24.0\t\t9D\n\r";

    sscanf(line1, "T%d=%f\t\t%X", &sensorid, &temperature, &checksum);
    printf("[1] SensorID = [%d], Temp = [%f], Checksum = [0x%X]\n",
sensorid, temperature, checksum);

    sscanf(line2, "T%d=%f\t\t%X", &sensorid, &temperature, &checksum);
    printf("[2] SensorID = [%d], Temp = [%f], Checksum = [0x%X]\n",
sensorid, temperature, checksum);
}

The output was:

[1] SensorID = [1], Temp = [-11.500000], Checksum = [0xEA]
[2] SensorID = [1], Temp = [24.000000], Checksum = [0x9D]

Your sscanf() call worked as expected.  Maybe your input isn't exactly
what you think (i.e. spaces instead of a tab)?

-- 
Adam Stein @ Xerox Corporation       Email: adam@ppdev.mc.xerox.com

Disclaimer: Any/All views expressed
here have been proven to be my own.  [http://www.csh.rit.edu/~adam/]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: sscanf trouble
  2011-01-25 11:08 Jonathan Andrews
@ 2011-01-25 12:18 ` Jonathan Wakely
  2011-01-25 12:21 ` Adam Stein
  1 sibling, 0 replies; 5+ messages in thread
From: Jonathan Wakely @ 2011-01-25 12:18 UTC (permalink / raw)
  To: jon; +Cc: gcc-help

On 25 January 2011 11:09, Jonathan Andrews wrote:
> Hi people.
>
> Can anyone help with what should be a simple problem. I have some text
> in the following form. Its a temperature reading from a wireless sensor,
> the format is T<sensor number>=<float ish> <TAB><TAB><Two digit ascii
> checksum>
>
> T1=18.0<TAB><TAB>XX\n\r
>
> Some examples:
>
>        T1=-11.5        EA
>        T1=24.0         9D
>
> Im trying to convert this string into a sensor ID, a floating point
> reading and a checksum as 3 variables using sscanf
>
> sscanf(line,"T%d=%f\t\t%X",&sensorid,&temperature,&checksum);
>
> No amount of variations on a theme seem to give me an entire decode
> here, seems the equals seems to screw things up.  Anyone any ideas how I
> can make this work?
>
> Thanks for any advice,
> Jon

This is just a general C question, unrelated to gcc, so there are more
relevant places to ask.
Asking C language questions on a compiler list is like asking a car
manufacturer for directions: gcc is just a tool you use to compile C.
Anyway ...

All you've said is something is screwed up, so it's no obvious what is
happening and why it doesn't match what you expect.  The sscanf call
should work, as long as 'line' really does match the format you
expect, sensorid is an int, checksum is an unsigned int, and
temperature is a float (not a double.)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* sscanf trouble
@ 2011-01-25 11:08 Jonathan Andrews
  2011-01-25 12:18 ` Jonathan Wakely
  2011-01-25 12:21 ` Adam Stein
  0 siblings, 2 replies; 5+ messages in thread
From: Jonathan Andrews @ 2011-01-25 11:08 UTC (permalink / raw)
  To: gcc-help

Hi people.

Can anyone help with what should be a simple problem. I have some text
in the following form. Its a temperature reading from a wireless sensor,
the format is T<sensor number>=<float ish> <TAB><TAB><Two digit ascii
checksum>

T1=18.0<TAB><TAB>XX\n\r

Some examples:

        T1=-11.5        EA
        T1=24.0         9D

Im trying to convert this string into a sensor ID, a floating point
reading and a checksum as 3 variables using sscanf

sscanf(line,"T%d=%f\t\t%X",&sensorid,&temperature,&checksum);

No amount of variations on a theme seem to give me an entire decode
here, seems the equals seems to screw things up.  Anyone any ideas how I
can make this work?

Thanks for any advice,
Jon




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-01-25 19:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-25 19:57 sscanf trouble Jonathan Andrews
  -- strict thread matches above, loose matches on Subject: below --
2011-01-25 18:18 Bill McEnaney
2011-01-25 11:08 Jonathan Andrews
2011-01-25 12:18 ` Jonathan Wakely
2011-01-25 12:21 ` Adam Stein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).