In order to prepare for the upcoming interview, the previous RPG project to install to the mobile phone run ran, found that the game will be stuck, simply depressed ...
When I finished this project, Android 5.0 was not released, I was tested on Android 2.3 to Android 4.4 device (as a poor force of my own mobile phone is 2.3. ), now finally changed mobile phone, brush CM12 (Android 5.0.2), sure enough, there is a bug, the game will be inexplicable card dead ...
Test for one hours, finally the error range locked in a read file function, and then 1.1 points in the comment function of some lines, and finally found that the SSCANF function caused the problem of the card, although the modification is very easy, but in the process of discovering the error of a lot of time, can only be wary of themselves, to do C + + Use the C library function carefully.
The Spit is so long, finally write a solution, although very simple.
As we all know, sscanf's function is to format the data from a string (char*), for example
1 Const Char* str ="Ten 3.14 haha";2 intx;3 Doubley;4 Charz[1024x768];5SSCANF (str,"%d%lf%s", &x, &y, z);6printf"%d%.2lf%s\n", x, y, z);
This code will output "Ten 3.14 haha".
Accordingly, there is the function sprintf formatted output to the string, the usage is similar, does not elaborate.
This is the C library function, in C + + corresponding to what should be done? can use StringStream to do, in fact, the use of the two C library function is simpler, so there is no need to use C library function. Rewrite the above code with StringStream:
1#include <sstream>2#include <iostream>3#include <string>4 5 using namespacestd;6 7 intMain () {8 Const Char* str ="Ten 3.14 haha";9 StringStream stream;TenStream <<str; One intx; A Doubley; - stringZ; -Stream >> x >> y >>Z; thecout << x <<" "<< y <<" "<< Z <<Endl; - return 0; -}
When using a stringstream multiple times, call Stream.str ("") to empty the unread stream and call Stream.clear () to clear the error token.
Feel the NDK is really fragile, often encounter with GCC and vs both without warning code, get Android on the Flash back, debugging the original correct code is really not a pleasant thing ...
sscanf function causes Android 5.0 to die, C C library function is used with caution