Reading and storing values from .OBJ files using C++ -
first, sorry bad english. well, i'm trying read values of .obj file (see here) , storing them in variables using program:
#include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; int main() { string line; string v, valuesx[8], valuesy[8], valuesz[8]; int n = 0; ifstream myfile ("cubo.obj"); while(!myfile.eof()) { getline (myfile,line); if (line[0] == 'v') { myfile >> v >> valuesx[n]>> valuesy[n]>> valuesz[n]; cout << valuesx[n] << "\t" << valuesy[n] << "\t" << valuesz[n] << endl; n++; } } return 0; } the file it's simple cube, exported blender. expected him show me lines beginning "v", result presents odd "v" lines. when read directly value of variable "line", result same. however, when remove line assigns values to variables "value" , read variable "line" directly, program works perfectly. know explain me happening? why program ignoring lines?
as mentioned in comment, read 1 line with:
getline (myfile,line); and if line has v in first position read line statement:
myfile >> v >> valuesx[n]>> valuesy[n]>> valuesz[n]; but not process previous line read in getline lose line. 1 possible solution process each line matches using istringstraeam:
std::istringstream iss( line ); iss >> v >> valuesx[n]>> valuesy[n]>> valuesz[n];
Comments
Post a Comment