Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sscanf compilation error #488

Closed
nicjohnston opened this issue Jun 29, 2015 · 30 comments
Closed

sscanf compilation error #488

nicjohnston opened this issue Jun 29, 2015 · 30 comments

Comments

@nicjohnston
Copy link

nicjohnston commented Jun 29, 2015

I recently tried to use sscanf, however the compiler threw an undefined reference error.
Replacing sscanf with os_sprintf as recommended in issue #404 did not solve it, instead it created a watchdog timer reboot.
Here is the code I was using:

void setup() {
  Serial.begin(115200);
  Serial.println("starting");
  char *data = "1234";
  int i = 0;
  sscanf(data,"%d", &i);
  Serial.println(i);
}
void loop() { }

Any suggestions would be appreciated.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@chadouming
Copy link
Contributor

You need os_scanf and not os_sprintf

On Mon, Jun 29, 2015, 7:04 PM nicjohnston notifications@github.com wrote:

I recently tried to use sscanf, however the compiler threw an undefined
reference error.
Replacing sscanf with os_sprintf as recommended in issue #404
#404 did not solve it, instead
it created a watchdog timer reboot.
Here is the code I was using:

void setup() {
Serial.begin(115200);
Serial.println("starting");
char *data = "1234";
int i = 0;
sscanf(data,"%d", &i);
Serial.println(i);
}void loop() { }

Any suggestions would be appreciated.


Reply to this email directly or view it on GitHub
#488.

@nicjohnston
Copy link
Author

Thanks for the reply, however I just tried os_scanf, and it threw the following error

error: 'os_scanf' was not declared in this scope

@chadouming
Copy link
Contributor

what are you trying to do ?
scanf is intended to read an input from the user. If you want to take the content of "i" and put it in the char buffer "data", then you would do :

void setup() {
Serial.begin(115200);
Serial.println("starting");
char *data = "";
int i = 0;
sprintf(data,"%d", i);
Serial.println(i);
}

Serial output should be :

Starting
0

Also, don't forget that sprintf on arduino doesnt support float or double. Just in case you wanted to use these types.

@nicjohnston
Copy link
Author

I am attempting to extract an integer value from a char buffer into a variable; I probably should have specified this sooner.
This issue surfaced while attempting to compile the ArduinoIMU example sketch from this library.
Does this esp8266 library have support for sscanf? If not, could you point me in the right direction to add it myself?
Thanks.

@chadouming
Copy link
Contributor

Ok, so this might be a dirty hack, but can you try :

char *buff2 = "24";
int i = ((String)(buff2)).toInt();

Worked fine for me even tho it made a warning.

@Links2004
Copy link
Collaborator

String is a class not a type.
warning free:

char buff2[] = "24";
int i = String(buff2).toInt();
Serial.println(i);

@nicjohnston
Copy link
Author

Thanks for reminding me about toInt, however the toInt function doesn't support parsing multiple variables from a char array.
This should have been in my previous post, but a more representative string is "1500,2100,1200,0". The example code used a single variable string to make debugging easier.
I could loop through the array and find every delimiting character in order to split it up into separate arrays and then convert those to integers using the toInt function, however I use sscanf to parse data in many of the sketches I would like to port to the ESP8266. Because of this, either a drop in replacement for sscanf or a patch for sscanf would be preferable.

@a-andreyev
Copy link

completely agree with @nicjohnston, we should implement siscanf(...)

@Toshik
Copy link
Contributor

Toshik commented Oct 23, 2015

Any news about sscanf?

@torntrousers
Copy link
Contributor

I'm looking to parse an HTTP Date header - "Thu, 15 Oct 2015 08:57:03 GMT" - into day, hours, minutes etc - I'd like sscanf too.

@lighthousebulb
Copy link

any ideas for a workaround yet?

@a-andreyev
Copy link

@lighthousebulb, I guess current ugly workaround is to use Arduino String.

@Toshik
Copy link
Contributor

Toshik commented Dec 13, 2015

@a-andreyev How do you suppose to use String instead of sscanf?

@andig
Copy link
Contributor

andig commented Jan 13, 2016

+1 for sscanf as its available on "standard" Arduino.

@nouser2013
Copy link
Contributor

Same issue. I'd need sscanf, too.

@gerardwr
Copy link

+1.Same issue. I'd need sscanf, too.

@Duality4Y
Copy link
Contributor

while this triggers wdt resets:

void setup() {
  Serial.begin(115200);
  Serial.println("starting");
  char *data = "1234";
  int i = 0;
  sprintf(data,"%d", &i);
  Serial.println(i);
}
void loop() { }

this doesn't:

void setup()
{
  Serial.begin(115200);
  Serial.println("starting");
  char data[20];
  int i = 0;
  sprintf(data, "%d", &i);
  Serial.println(i);
}

void loop()
{

}

@Links2004
Copy link
Collaborator

@Duality4Y "1234" is const char and can not be modified.
note: your issues has noting to do with sscanf

@Duality4Y
Copy link
Contributor

i am just saying that sprintf works, but the way nicjohnston was using it (probably) was with a const char * and that doesn't work.

I assumed he just replaced his sscanf with sprintf.
(he mentions this in the first post)

@Links2004
Copy link
Collaborator

sscanf and sprintf have totally different functions using as a replaced is not possible.
the SDK not provide a sscanf Implementation, and its not a small functionality.
if some one find code that do sscanf under LGPL licence (or compatible) we can adapted it to the ESP.

@Duality4Y
Copy link
Contributor

I see my mistake :) sorry for the confusion!

if I understand it correctly he wants to convert a series of comma seperated values to series of ints.
you could probably do that with atoi and strtok_r.
little bit advanced though :)

but yea sscanf would be nice to have!

@Duality4Y
Copy link
Contributor

ok here is a thing i copy pasted together (me being way lazy), this works on your case.
this is not tested at all!
it came from here: http://mirror.fsf.org/pmon2000/2.x/src/lib/libc/
and it is probably tested there.
I can't garantee that it doesn't have bugs. :)

http://pastebin.com/N4d029VQ

@igrr
Copy link
Member

igrr commented Mar 11, 2016

Implemented in #1752

@torntrousers
Copy link
Contributor

Woohoo, thank you!

@igrr igrr added this to the 2.4.0 milestone Jun 23, 2016
@igrr
Copy link
Member

igrr commented Jun 23, 2016

Merged and available in git version.

@skorokithakis
Copy link
Contributor

I would really like to use this, how can I try it out? I checked out the latest feature/libc head to PlatformIO's directory but I just got "undefined reference to sscanf"...

@electronicsguy
Copy link

@igrr Has sscanf() been implemented in the latest build? I still get undefined reference to sscanf error.

@bebo-dot-dev
Copy link

please see #3120 (comment)

@electronicsguy
Copy link

@Duality4Y Thanks Robert! This helped in the meantime. It doesn't work as is (compilation errors due to some strange blank characters in that file). I've cleaned it up at put it here for anyone who needs it: sscanf.h & sscanf.cpp. Cheers 👍

@AnishDey27
Copy link

AnishDey27 commented Jul 19, 2021

With arduino IDE while using NODEMCU variables are defined as 32 bit ( only "int" ) may be anyone defined it as int16_t or anything else. I used 32 bit and that solved my problem with sscanf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests