Search code examples
c++stringscanf

C++ using std::sscanf and std::string


I am wanting to "extract" the info from a string. The string is always in the format int int char.

I have spent solid hours on this, checked "every" example this site and google I found, to no avail. Some examples compiled, but crashed (no overflow.)

Here is the current, it compiles but crashes.

// Data                    
string str = "53 25 S";
int num1;
int num2;
char type3;

// Read values                                  
sscanf(str.c_str(),"%i %i %c",num1,num2,type3);

Solution

  • You need the address of operator i.e.

    sscanf(str.c_str(),"%i %i %c",&num1,&num2,&type3);