0

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));
timrau
  • 22,578
  • 4
  • 51
  • 64
FPGA
  • 3,525
  • 10
  • 44
  • 73

3 Answers3

1

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));
timrau
  • 22,578
  • 4
  • 51
  • 64
1

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)

C. Stoll
  • 376
  • 1
  • 6
0

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

PermanentGuest
  • 5,213
  • 2
  • 27
  • 36