Search code examples
c++parsingistringstream

c++ istringstream


i am trying to write a c++ opengl application to load .OBJ files and i use this code to read the vertex:

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;
}

so basicly the second pice of code parses the vertex lines from the obj file

v 1.457272 0.282729 -0.929271

my question is how can i parse this vertex line c++ style using istringstream what would the code below translate to in c++ syntax

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

Solution

  • If you are sure that the first 2 characters are useless:

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