Archived

This topic is now archived and is closed to further replies.

DmGoober

sscanf failing on MS's sample code!

Recommended Posts

Microsoft uses the following code to demonstrate sscanf: char tokenstring[] = "15 12 14..."; char s[81]; char c; int i; float fp; /* Input various data from tokenstring: */ sscanf( tokenstring, "%s", s ); sscanf( tokenstring, "%c", &c ); sscanf( tokenstring, "%d", &i ); sscanf( tokenstring, "%f", &fp ); /* Output the data read */ printf( "String = %s\n", s ); printf( "Character = %c\n", c ); printf( "Integer: = %d\n", i ); printf( "Real: = %f\n", fp ); However, upon reaching the line where sscanf tries to separate a float, I get the following error: Debug Error! Program G:\CONVERT\DEBUG\CONVERT.EXE runtime error (Press Retry to Debug the App) Pressing retry does nothing, as MSVC++ simple dumps the assembly of SSCANF. I have tried messing around with the code -- the weird thing is that it works sometimes. Floats seem to make the program choke maybe 3/4 of the times I run it. Any hints? Thanks.

Share this post


Link to post
Share on other sites
Solved the problem. Here was the actual problem code:

float monkey;
sscanf ("1.5", "%f", &monkey);

Because the Visual C++ does not know what sscanf is really doing (sscanf''s behavior is determined by the format specifiers) vc++ did not know that floating point numbers were being used. Therefore, as an optimization, it did not load floating pt libraries!

Duh!

So the solution is to add this:
float monkey = 10.0;
monkey = 0.0;
sscanf ("1.5", "%f", &monkey);

Fun huh? Nothing like a bit of extra worthless code to make the compiler work correctly, huh?

DmGoober

Share this post


Link to post
Share on other sites