petez69
September 15, 2018, 10:12am
1
Hi Folks
I have a MQQT received topic:
cmnd/20/Output/17 received by my a callback as I'm subscribed to cmnd/20/+ via broker
I can successfully extract the integers needed with: ie 20 and 17
sscanf(topic,"%[^/]/%d/% [^/]/%d",&tempInt,&OutputNumber);
tempInt = 20
OutputNumber = 17
What I want to do is also extract the text also into variables....
sscanf(topic,"%s[^/]/%d[^/]/%s[^/]/%d",tempStr,&tempInt,tempStr1,&OutputNumber);
regex should discard all "/"
this doesnt extract the data and screws up tempInt and OutputNumber...I've tried all different permutations here I can think of....
I'd appreciate a hint as to where I'm going wrong.....
Thankyou..pete
darrob
September 15, 2018, 10:30am
2
You could use strtok (see here and here ) to break the string into substrings, then process each substring
petez69
September 15, 2018, 10:38am
3
Hi there, yes I could use a different command and not learn how to use sscanf properly. I'd prefer to persist with a little guidance on this specific command...
Thankyou...Pete
darrob
September 15, 2018, 11:00am
4
Sorry for my deleted reply
You have
sscanf(topic,"%s[^/]
try
sscanf(topic,"%[^/]s
petez69
September 15, 2018, 11:20am
5
Thanks for the reply
sscanf(topic,"%[^/]s/%d/%[^/]s/%d",tempStr,&tempInt,tempStr1,&OutputNumber)
parses out cmnd for tempStr so thats an improvement HOWEVER the rest of the values are not parsed correctly.
So you got me on the right track
sscanf(topic,"%[^/]/%d/%[^/]/%d",tempStr,&tempInt,tempStr1,&OutputNumber);
That works......I would have thought I needed a %s for the string however with it, fail.. Removing the s for string, it works....
Weired but thankyou !!!
Pete