Question

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));
Was it helpful?

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

OTHER TIPS

It works just like reading from the standard input:

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

(note: the tmpis just there to eat the 'v' at the beginning of the line and can be ignored - alternatively cut it out before/while initialising the stream)

See here. This gives a good example. Use it to adapt your code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top