我想扫描像
"[25, 28] => 34"
我写了一个小程序来测试它:
#include <cstdlib>
#include <iostream>
int main() {
char* line = "[25, 28] => 34";
char a1[100], a2[100];
int i;
sscanf(line, "[%[^,], %[^\]] => %i", a1, a2, &i);
std::cout << "a1 = " << a1 <<"\na2 = " << a2 << "\ni = "<<i <<"\n";
return 0;
}
编译这个给出
warning: unknown escape sequence '\]'
和输出
a1 = 25
a2 = 28
i = -1073746244
如果我将其更改为
sscanf(line, "[%[^,], %[^]] => %i", a1, a2, &i);
我没有收到编译器投诉,但仍然
a1 = 25
a2 = 28
i = -1073746244
我知道问题出在第二个标记上,因为
sscanf(line, "[%[^,], %[0123456789]] => %i", a1, a2, &i);
给
a1 = 25
a2 = 28
i = 34
但我想对第二个令牌使用终止条件。我怎么做?