Go Down

Topic: sscanf - ignoring characters (Read 3862 times) previous topic - next topic

Marian

Hello,

obviousely I don't understand something:

I have a string : "pH=8.23" and I want to ignore the first three characters: "pH="

so I thought that :

char inData_pH;
char string_pH[8];
sstring(inData_pH, "%*3c %s", string_pH)  // i.e. from inData_pH ignore the 3 characters and assign rest to string_pH
pH_val = atof(string_pH);

can you guys help a bit ?

cheers,
Marian

PaulS

#1
Jan 22, 2011, 12:53 am Last Edit: Jan 22, 2011, 12:53 am by PaulS Reason: 1
sstring? Did you mean scanf?

Marian

Paul,

my sincere regrets. Has been a long week ;)
Yes I meant sscanf:

char inData_pH;
char string_pH[8];
sscanf(inData_pH, "%*3c %s", string_pH)  // i.e. from inData_pH ignore the 3 characters and assign rest to string_pH
pH_val = atof(string_pH);


PaulS

The first argument to sscanf is an array, not a character. It is the string to be scanned.

This worked for me:
Code: [Select]
void setup()
{
 Serial.begin(9600);
 
 char inData[] = "PH=8.23";
 Serial.print("String to be scanned: [");
 Serial.print(inData);  Serial.println("]");
 
 char phValue[12];
 phValue[0] = '\0';
 
 sscanf(inData, "%*3c%s", &phValue);
 Serial.print("String of interest extracted: [");
 Serial.print(phValue);  Serial.println("]");
}

void loop()
{
}


Output:
Quote
String to be scanned: [PH=8.23]
String of interest extracted: [8.23]

robtillaart


If it are allways 3 characters you want to ignore why not just using strcpy?

Code: [Select]
void setup()
{
 Serial.begin(9600);

 char inData[] = "PH=8.23";
 char phValue[12];

 strcpy(phValue, &inData[3]);

 Serial.print("["); Serial.print(phValue);  Serial.println("]");
}

void loop()
{
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy