programma loopt vast bij lezen van een textbestand

Status
Niet open voor verdere reacties.

jopie33

Gebruiker
Lid geworden
10 apr 2010
Berichten
30
hallo,

Ik ben een programma aan het schrijven die een tekstbestand leest. Het meeste werk is gedaan, maar nu moet ik dus de eerste 3 coordinaten lezen uit de regels die met de letter f beginnen. Deze worden vervolgens in een variabele gezet.

* Zie bijlage voor het tekstbestand*

dus bij de regel f 6/2/3 5/3/3 1/4/3 wordt dit naar de volgende variabelen geschreven:
triangles.x = 6;
triangles.y = 2;
triangles.z = 3;

(triangles is een array van objecten en het aantal objecten in deze array is gelijk aan het aantal regels die beginnen met een f. variabele i is de tel variabele in de for loop. Zie de code hieronder.)


[CPP]
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

struct triangle{
float x;
float y;
float z;

float nx;
float ny;
float nz;

float u;
float v;
};


int totalVertices = 0;
int totalNormals = 0;
int totalTexCoords = 0;
int totalFaces = 0;



void loadobj(string sfilename)
{
vector<string> line;
string curLine = "";

ifstream infile(sfilename.c_str());
if (!infile.is_open() || !infile.good()){
cout << "Fout met openen!";
return;
}

cout << " * " << sfilename << " opened" << " * " << endl << endl;

string temp;
size_t found;

while (getline(infile, curLine)){
line.push_back(curLine);
}

for(unsigned i = 0; i < line.size() - 1; i++){
if(line[0] == '#')cout << "comment" << endl;
else if(line[0] == ' ')cout << "empty line" << endl;
else if(line[0] == 'v' && line[1] == ' ')cout << "vertex" << endl;
else if(line[0] == 'v' && line[1] == 'n')cout << "vertex normal" << endl;
else if(line[0] == 'v' && line[1] == 't')cout << "texture coordinate" << endl;

else if(line[0] == 'f')cout << "face" << endl;
else if(line[0] == 's')cout << "smooth shading" << endl;
else if(line[0] == 'g')cout << "group" << endl;
else cout << line << endl;


found = line.find("vertices");
if(found != string::npos){
temp = line.substr(2, int(found) - 3);
istringstream ss(temp);
ss >> totalVertices;
}

found = line.find("vertex normals");
if(found != string::npos){
temp = line.substr(2, int(found) - 3);
istringstream ss(temp);
ss >> totalNormals;
}

found = line.find("texture coords");
if(found != string::npos){
temp = line.substr(2, int(found) - 3);
istringstream ss(temp);
ss >> totalTexCoords;
}

found = line.find("faces");
if(found != string::npos){
temp = line.substr(2, int(found) - 3);
istringstream ss(temp);
ss >> totalFaces;
}
}

cout << " * " << sfilename << " closed" << " * " << endl;

cout << endl;
cout << "totalVertices = " << totalVertices << endl;
cout << "totalNormals = " << totalNormals << endl;
cout << "totalTexCoords = " << totalTexCoords << endl;
cout << "totalFaces = " << totalFaces << endl << endl;

cout << " * " << "Creating vertex, normal, texture coordinate and triangle arrays" << " * " << endl;

triangle triangles[totalFaces];

int a1,a2,a3,b1,b2,b3 = 0;

for(int i = 0; i < line.size(); i++){


stringstream sv1;
stringstream sv2;
stringstream sv3;

a1 = 0;
a2 = 0;
a3 = 0;
b1 = 0;
b2 = 0;
b3 = 0;

if(line[0] == 'f' && line[1] == ' '){

while(line[a1] == 'f' || line[a1] == ' ')a1++;

b1 = a1;

while(line[b1] != '/')b1++;
cout << "a1 = " << a1 << endl;
cout << "b1 = " << b1 << endl;

sv1.str(string());
temp = line.substr(a1, b1-a1);
sv1.str(temp);
sv1 >> triangles.x;

a2 = b1 + 1;

while(line[a2] == '/')a2++;

b2 = a2;

while(line[b2] != '/')b2++;

cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl;

sv2.str(string());
temp = line.substr(a2, b2-a2);
sv2.str(temp);
sv2 >> triangles.y;

a3 = b2 + 1;

while(line[a3] == '/')a3++;

b3 = b2;

while(line[b3] != ' ')b3++;

cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;

/*
sv3.str(string());
temp = line.substr(a3, b3-a3);
sv3.str(temp);
sv3 >> triangles.z;
*/

cout << "x" << triangles.x << "x ";
cout << "y" << triangles.y << "y ";
cout << "z" << triangles.z << "z" << endl;
}
}
}

int main ()
{
int keuze;
string filename;

cout << "filename: ";
cin >> filename;
cout << endl << endl;

loadobj(filename);

cout << endl << endl << "press enter to close";

cin.ignore();
cin.get();

return 0;
}

[/CPP]


Het probleem het volgende:

Zoals je ziet is van het volgende stukje een comment gemaakt.
[CPP]
/*
sv3.str(string());
temp = line.substr(a3, b3-a3);
sv3.str(temp);
sv3 >> triangles.z;
*/
[/CPP]

Dit stukje moet er voor zorgen dat het laatste coordinaat (coordinaat z) wordt gelezen en in de daarbij horende variabele wordt geplaatst (triangles.z). Alleen als ik dit uncomment dan compiled het programma wel en leest het deze coordinaten in, maar dan loopt het programma vast. Is dit stukje gecomment, dan werkt mijn programma prima, maar wordt de z coordinaat dus niet ingeladen. Dit moet wel gebeuren.
Hoe moet ik dit oplossen?
 

Bijlagen

Laatst bewerkt:
Je gebruikt variabele i om door de regels heen te gaan.
Code:
for(int i = 0; i < line.size(); i++){
Echter indexeer je hier ook de array triangles mee.
bijv:
Code:
sv1 >> triangles[i].x;
Deze array heeft lengte totalFaces. Je krijgt dan een overflow.
Het tekstbestand heeft nl. in totaal meer regels dan regels die met een
f beginnen. ( i >= totalFaces )
Bovendien worden dan niet alle elementen van de array gebruikt.

Om de array te vullen is een aparte index nodig.
Boven de for loop kan je een aparte index voor de array declareren
Code:
int iface = 0;
for(int i = 0; i < line.size(); i++){
triangles vervangen door
triangles[iface]

en na de cout de index verhogen
Code:
cout << "z" << triangles[iface].z << "z" << endl;
iface++;


Je kan evt. ook een vector gebruiken voor triangles.
 
Laatst bewerkt:
C er, heel erg bedankt :thumb: Daar was ik zelf nooit opgekomen.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan