Pages: [1]   Go Down
Author Topic: sscanf - ignoring characters  (Read 2496 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sstring? Did you mean scanf?
« Last Edit: January 21, 2011, 06:53:58 pm by PaulS » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 40
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

my sincere regrets. Has been a long week smiley-wink
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);

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This worked for me:
Code:
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]
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 170
Posts: 12460
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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()
{
}
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: