【问题标题】:c++ istringstreamc++ istringstream
【发布时间】:2012-07-25 14:15:51
【问题描述】:

我正在尝试编写一个 c++ opengl 应用程序来加载 .OBJ 文件,我使用此代码来读取 顶点:

char buf[256];
while(!objFile.eof())
{
    objFile.getline(buf,256);
    coordinates.push_back(new std::string(buf));
}

else if ((*coordinates[i])[0]=='v' && (*coordinates[i])[1]==' ')
{
    float tmpx,tmpy,tmpz;
    sscanf(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
    vertex.push_back(new coordinate(tmpx,tmpy,tmpz));
    cout << "v " << tmpx << " " << tmpy << " " << tmpz << endl;
}

所以基本上第二段代码会解析 obj 文件中的顶点行

v 1.457272 0.282729 -0.929271

我的问题是如何使用istringstream 解析此顶点线 c++ 样式 下面的代码在 c++ 语法中会翻译成什么

sscanf(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz);
    vertex.push_back(new coordinate(tmpx,tmpy,tmpz));

【问题讨论】:

  • coord 未在附加代码 sn-p 中声明。

标签: c++ parsing istringstream


【解决方案1】:

如果你确定前 2 个字符没用:

istringstream strm(*coord[i]);
strm.ignore(2);
strm >> tmpx >> tmpy >> tmpz;
vertex.push_back(new coordinate(tmpx, tmpy, tmpz));

【讨论】:

    【解决方案2】:

    它就像从标准输入中读取一样工作:

    istringstream parser(coord[i]);
    char tmp;
    parser >> tmp >> tmpx >> tmpy >> tmpz;
    

    (注意:tmp 只是在行首吃“v”,可以忽略 - 或者在初始化流之前/同时将其剪掉)

    【讨论】:

      【解决方案3】:

      here。这是一个很好的例子。用它来调整你的代码

      【讨论】:

      • jcatki.no-ip.org/fncpp/cplusplus.com
      猜你喜欢
      • 1970-01-01
      • 2019-04-28
      • 2011-02-15
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      相关资源
      最近更新 更多