gpt4 book ai didi

c++ - 没有匹配函数调用 sscanf

转载 作者:太空宇宙 更新时间:2023-11-04 16:16:59 25 4
gpt4 key购买 nike

为什么我在使用 sscanf 时会收到此错误? , 包括 <cstdio>并打开了file

我在做主要的事情:

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

出现错误 No matching function for call to 'sscanf' .

sscanf引用:http://www.cplusplus.com/reference/cstdio/sscanf/

最佳答案

sscanf() 是一个 C 函数。第一个参数应为 const char * 类型。您正在向它传递一个 C++ string。由于类型不匹配,编译器会报错(它应该报错)。

试试这个:

sscanf( line.c_str(), "%d", &i )

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

file >> i

关于c++ - 没有匹配函数调用 sscanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21699684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com