Hallo,
I am working on a function for reading large amount of data from a file. Each line contains the information needed to build a triangle. What is the best way to do this?
Right now Im tying to read each line, convert it into a float and than create a triangle before I read the next line. Is there a smarter way? Would be great to get some input on this.
I also have a problem with the printing. I get a lot of random characters after i have printed the numbers.Code:std::string currentLine; // Variable that holds the current line // we are using. int nrLines = 0; // Variable for checking how many lines // there are in the file // Open file std::ifstream myFile (FileName); /* // The content of the file random line 11 ra V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411 V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411 V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411 V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411 */ // An array of strings, for reading the 3 vertexes we will later // use. Note that there is 3 vertexes, each made up of 3 // cordinates std::string vertex[3][3]; if (myFile.is_open()) { // While the file is open, we read the info and construct triangles while (! myFile.eof()) { // Read line getline(myFile, currentLine); // Check if this line holds vertex information. If the line starts // with V, we will use it. if (currentLine[0] == 'V') { // Break the line into different components, so that we can // create 3 different vertexes for each cordinate in the file // V: 0.0,0.0,0.0 1.0,1.0,1.0 2.0,2.0,2.0 // Variable for keeping track of which character in currentLine // we are reading, it starts at 2, as the 3 first characters // are not part of the number we are tring to read int x = 3; // Make a loop for creating 3 vertexes for (int i = 0; i < 3; i++) { // Loop for looping throught the three cordinates of each // vertex for (int j = 0; j < 3; j++) { // Make sure the vertex string we are writing to now is // empty vertex[i][j].clear(); // Make sure the current character is part of the number while (currentLine[x] != ',' && currentLine[x] != ' ') { // If the current character is part of a number, // append it to the vertex string vertex[i][j].push_back(currentLine[x]); x++; } x++; } } // Convert the string into floats } // Increment the line counter nrLines++; } // Finished with reading the file, close it myFile.close(); }
Thanks for your timeCode:// Print the thing for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { std::cout << vertex[i][j] << " "; } std::cout << " - "; } std::cout << std::endl;
EDIT:
Right now Im trying to load my own file format, which kind of suck. Does anyone know where to find good information on how to load .obj files? I have looked, but could not find anything good (simple)
Thanks again



LinkBack URL
About LinkBacks



