没有用于调用 sscanf 的匹配函数

OJF*_*ord 0 c++ scanf

为什么我收到这个错误sscanf,包括<cstdio>和打开file

我主要做:

string  line;
int     num = 0, i;
while(getline(file, line)){
    ++num;
    if (sscanf(line, "%d", &i) == 0) --num;
}
Run Code Online (Sandbox Code Playgroud)

并得到错误No matching function for call to 'sscanf'

sscanf参考:http : //www.cplusplus.com/reference/cstdio/sscanf/

Dev*_*lar 6

sscanf()是一个C函数。第一个参数的类型应该是const char *。你正在传递一个C++ string。由于类型不匹配,编译器会抱怨(应该如此)。

尝试这个:

sscanf( line.c_str(), "%d", &i )
Run Code Online (Sandbox Code Playgroud)

更好的是,使用更健壮的C++ I/O

file >> i
Run Code Online (Sandbox Code Playgroud)