[SOLVED] Sscanf not working like it should

I can’t get sscanf to parse a string correctly.

Check out the code below, and the output:

Code

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);


void setup(){
   Serial.begin(115200);
   while(!Serial.available()){};

   uint16_t r1, r2, r3;
   unsigned int r4;
   uint16_t r5, r6, r7;
   float r8;

   const char* toParse = "1,1,0,1493711570,666,777,1234,  1.23\r\n";

   int result = sscanf(toParse,
         " %u,"" %u,"" %u,"" %lu,"" %u,"" %u,"" %u,"" %f\r\n",
            &r1, &r2,  &r3,   &r4,  &r5,  &r6,  &r7,   &r8
   );

   Serial.printf("result == %d ... parsed values are: \r\n", result);
   Serial.printf(      "  Parsed: %u,""%u,""%u,""%lu,""%u,""%u,""%u,""%f\r\n",
                           r1,   r2,   r3,    r4,   r5,   r6,   r7,    r8);
   Serial.printf("Original: %s\r\n\r\n", toParse);


}

void loop(){

}

Output

result == 7 ... parsed values are:

  Parsed: 1,1,0,1493696512,666,777,1234,0.000000

Original: 1,1,0,1493711570,666,777,1234,  1.23

_
_
Yet when I run the exact same code in C++ shell I get the correct parsing (see working program here) :

result == 8 … parsed values are:
Parsed: 1,1,0,1493711570,666,777,1234,1.230000
Original: 1,1,0,1493711570,666,777,1234, 1.23

2 Likes