尝试用sstream替换scanf
Trying to replace scanf with sstream
我正在尝试解析一个文件。我目前正在使用以下代码,但希望将其替换为基于sstream的解决方案
unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
const char* cstring = line.c_str();
int matches = sscanf(cstring, "f %d/%d/%d %d/%d/%d %d/%d/%dn", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
当我尝试以下操作时,我得到了意想不到的结果
std::stringstream f(line);
f >> vertexIndex[0] >> uvIndex[0] >> normalIndex[0] >> vertexIndex[1] >> uvIndex[1] >> normalIndex[1] >> vertexIndex[2] >> uvIndex[2] >> normalIndex[2];
我肯定我误解了流的使用…任何帮助都太棒了!
这纯粹是缺乏理解。这个问题的解决方案是:
std::stringstream ss(line.substr(2));
char throwaway;
for (int i = 0; i < 3; i++){
ss >> vertexIndex[i] >> throwaway >> uvIndex[i] >> throwaway >> normalIndex[i];
}
我的下一个问题是为什么下面的不工作?
ss.ignore(std::numeric_limits<std::streamsize>::max(), '/');
ss >> vertexIndex[i] >> uvIndex[i] >> normalIndex[i];
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 如何用转义符替换字符串中的所有特殊字符
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 在一个读写器队列中,我可以用volatile替换原子吗
- 用符号版本替换对函数的所有调用
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 替换基于地图的所有引用
- 按平均值替换数组中的元素
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 初始化 std::vector 替换为单大括号而不是双大括号
- scanf() 只取数字作为输入,避免使用其他字符
- 删除/替换C++字符串中的多字符 (ÿû)
- 将 malloc 替换为数组
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 如何在C++中用'\'替换''来处理转义序列?
- 替换密码:哪一个?
- 替换 C++17 中移除的绑定 1st
- 尝试用sstream替换scanf