Attempting to read a .txt file

1

Well I'm trying to read from a .txt file, but when I try to read and by in variables it's giving me the whole wrong info, now wrong places. I've tried using Debug's tool but I can not figure out why ...

Here is the .txt file:

15  
29  
1;100;100;1  
2;150;150;1  
3;230;200;2  
4;100;250;2  
5;366;84;1  
6;565;74;1  
7;840;118;1  
8;1018;313;3  
9;971;502;3  
10;491;561;2  
11;844;399;2  
12;651;305;2  
13;387;319;2  
14;174;424;2  
15;172;561;1  

First line is the vertices, the second the edges. The other lines are:
number of vertex; position x; position and; type of vertex

My code to read file:

bool Grafo::Load(const string &fich_grafo, const string &fich_pessoas) {
// Abrir o ficheiro
ifstream in_file(fich_grafo);

// Variaveis auxiliares
string buffer;
int x = 0, y = 0, vertice = 0, tipo = 4, parametro = 0;

// Verificar se o ficheiro foi aberto corretamente  
if (!Uteis::FicheiroAberto(&in_file)) {
    cout << "Nao foi possivel abrir o ficheiro " << fich_grafo << endl;
    exit(1);
}

// Retirar os vertices
getline(in_file, buffer);
n_vertices = atoi(buffer.c_str());

// Retirar as arestas
getline(in_file, buffer);
n_arestas = atoi(buffer.c_str());

while (!in_file.eof()) {
    getline(in_file, buffer);
    stringstream ss(buffer);
    parametro = 0;
    while (getline(ss, buffer, ';')) {

        if (parametro == 0) {
            ss >> vertice;
        }
        if (parametro == 1) {
            ss >> x;
        }
        if (parametro == 2) {
            ss >> y;
        }
        if (parametro == 3) {
            ss >> tipo;
        }
        parametro++;
    }
    cout << vertice << " " << x << " " << y << " " << tipo << endl;
}
cout << "Vertices: " << n_vertices << " Arestas: " << n_arestas << endl;
return NULL;
}

The output is:

100 100 1 4
150 150 1 4
230 200 2 4
100 250 2 4
366 84 1 4
565 74 1 4
840 118 1 4
1018 313 3 4
971 502 3 4
491 561 2 4
844 399 2 4
651 305 2 4
387 319 2 4
174 424 2 4
172 561 1 4
422 623 1 4
722 693 1 4
1118 313 3 4
1139 258 3 4
1146 179 2 4
1117 102 2 4
1045 36 3 4
976 195 3 4
928 93 1 4
639 452 1 4
639 452 1 4
2 45 1 4
4 50 1 4
5 50 1 4
13 60 1 4
10 60 1 4
10 60 1 4
16 60 1 4
15 51 1 4
6 32 1 4
5 23 1 4
13 8 1 4
11 6 1 4
8 5 1 4
9 10 1 4
7 60 1 4
11 34 1 4
16 60 1 4
4 9 1 4
6 42 1 4
17 9 1 4
9 42 1 4
18 42 1 4
3 40 1 4
4 42 1 4
19 42 1 4
20 42 1 4
21 42 1 4
22 42 1 4
23 42 1 4
19 42 1 4
24 42 1 4
Vertices: 25 Arestas: 29

I can not figure out the problem, because there is no default in the output. Thank you very much.

SOLVED: I found the problem, I was recording the wrong variable.

while (!in_file.eof()) {
    getline(in_file, buffer);
    stringstream ss(buffer);
    parametro = 0;
    while (getline(ss, buffer, ';')) {
        stringstream aux(buffer);
        if (parametro == 0) {
            aux >> vertice;
        }
        if (parametro == 1) {
            aux >> x;
        }
        if (parametro == 2) {
            aux >> y;
        }
        if (parametro == 3) {
            aux >> tipo;
        }
        parametro++;
    }
    cout << vertice << " " << x << " " << y << " " << tipo << endl;
}
    
asked by anonymous 01.11.2018 / 22:31

0 answers