以下两行代码的另一个等效C++

another C++ equivalent for the followingtwo lines of code

本文关键字:另一个 C++ 代码 两行      更新时间:2023-10-16

很抱歉标题如此模糊。基本上,我正在尝试破解一个功能以满足我的需求。但是最近我一直在python上做很多工作,我的c ++有点生疏。

所以早些时候我的函数花了

 int func(FILE *f)  
 { .....
   if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL) 
    return MM_PREMATURE_EOF;
if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type, 
    storage_scheme) != 5)
    return MM_PREMATURE_EOF;
 }

现在我直接输入字符串数据而不是这个

 int func(std::string *data)  
   { .....
   // how should I modify this if statment..I want to parse the same file
   // but instead it is in form of one giant string
 }

谢谢

您可以使用相同的代码,只需将std::string中的数据转换为 C 字符串即可。

sscanf(data->c_str(), "%s %s %s %s %s", //...);

但是,您应该考虑传入 const 引用,因为您可能不打算修改输入数据:

int func(const std::string &data) {
    //...
    if (sscanf(data.c_str(), //...)) {
        //...
    }
}
相关文章: