Problem reading file

0

I am writing a code in C ++ and need to read a distance file that was calculated in another algorithm done in C .

This file looks something like this:

100 \ n (natural number) 0.0000 58.25646 7.1556 5.1564 0.0000 44.00000 ... - > an array of floats

Here is the code for the part that reads the file:

int main(){
    srand(time(NULL));

    FILE *arv;
    arv = fopen("distancias_oliver30.txt", "r");

    fscanf(arv,"%d", &N_geracoes);



    float distancias[N_cidades][N_cidades];
    float feromonio[N_cidades][N_cidades];

    int i, j, k;
    for (i = 0; i < N_cidades; i++){
        for (j = 0; j < N_cidades; j++){
            fscanf(arv,"%d",&distancias[i][j]);

    }


    fclose(arv);
    ...
The problem is that this first natural number reads correctly, but at distances it reads the first element correctly and the rest it throws a weird negative number (-107374176) instead of 58.25646, 5.1564 ... < p>

Does anyone know how to solve it?

    
asked by anonymous 30.09.2016 / 03:14

1 answer

2

Assuming this statement:

float distancias[N_cidades][N_cidades];

This is wrong:

fscanf(arv,"%d",&distancias[i][j]); // ERRADO!

This is the right way to read floats:

fscanf(arv,"%f",&distancias[i][j]); // CERTO :)
    
30.09.2016 / 03:20