小编sta*_*sse的帖子

如何将 scanf 精确匹配转换为现代 C++ stringstream 读取

我目前正在开发一个项目,我想使用现代 cpp 而不是依赖旧的 c 来读取文件。对于上下文,我正在尝试读取波前 obj 文件。

我有这个旧的代码片段:

const char *line;
float x, y, z;
if(sscanf(line, "vn %f %f %f", &x, &y, &z) != 3)
    break; // quitting loop because couldn't scan line correctly
Run Code Online (Sandbox Code Playgroud)

我已经翻译成:

string line;
string word;
stringstream ss(line);
float x, y, z;
if (!(ss >> word >> x >> y >> z)) // "vn x y z"
    break; // quitting loop because couldn't scan line correctly
Run Code Online (Sandbox Code Playgroud)

不同之处在于我使用 astring来跳过第一个单词,但我希望它与do"vn"一样匹配sscanf。这是可能的stringstream还是我应该继续依赖sscanf …

c++ scanf stringstream

4
推荐指数
1
解决办法
317
查看次数

标签 统计

c++ ×1

scanf ×1

stringstream ×1