我想从字符串中"提取"信息.字符串始终采用格式int int char.
我花了很多时间在这上面,检查了这个网站和谷歌我发现的"每个"例子,但无济于事.编译了一些示例,但崩溃了(没有溢出.)
这是当前的,它编译但崩溃.
// Data
string str = "53 25 S";
int num1;
int num2;
char type3;
// Read values
sscanf(str.c_str(),"%i %i %c",num1,num2,type3);
Run Code Online (Sandbox Code Playgroud)
您需要运营商的地址,即
sscanf(str.c_str(),"%i %i %c",&num1,&num2,&type3);
Run Code Online (Sandbox Code Playgroud)
简单阅读任何基本文本和文档sscanf(),您可以自己回答这个问题.
如果你真的坚持使用sscanf(),然后的地址num1,num2以及num3需要传递,而不是它们的值.
sscanf(str.c_str(),"%i %i %c",&num1,&num2,&type3);
Run Code Online (Sandbox Code Playgroud)
使用stringstream(在标准头中声明<sstream>)比尝试使用C中的弃用函数更好.
std::istringstream some_stream(str);
some_stream >> num1 >> num2 >> type3;
Run Code Online (Sandbox Code Playgroud)