我正在尝试编写一种方法,将 2 个变量设置为 char 字符串中的数字。该字符串看起来像:
[-][1][ ][ ][ ][ ][2][.][0][4]
可以提取数字 -1 和 2.04。我知道方法签名可能看起来像这样:
sscanf(array[i],"%d%f",&someint,&somedouble)
但老实说,我不确定如何着手编写它。任何帮助将不胜感激
最佳答案
你快到了:
sscanf(array[i], "%d %lf",&someint, &somedouble)
其中空格表示“0、1 或多个任意空白字符”
但是如果您使用的是 C++ 而不是 C,最好从 C++ 流开始。这会容易得多。
#include <sstream>
std::istringstream my_number_stream(array[i]);
if (!(my_number_stream >> std::skipws >> someint >> somedouble)) {
// oh noes ! an error !
}
关于c++ - 从 char 字符串中提取 2 个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3699163/