简体   繁体   中英

reading from file to array in C

I am having some issues when trying to get hold of the data in a text file in the next form:

12
1 1 1 1 1 1 0 0 0 0 0 0
3.4
4.0
2.5
3.4
1000
1500
1000 

As you can see, the first figure in the first line is always the amount of elements in the second line. My hideous code is like this so far:

parametros=fopen("lyapunov.params", "r");
    if(parametros == NULL){
        printf("\nSe toman valores por defecto.\n");
    }else{  //Se asume que el fichero tiene las lineas correctas.
        for(line=0; line<9; line++){
            if(line == 0){
                fscanf(parametros, "%d", seq_length);
            }
            if(line == 1){
                fscanf(parametros, "%d", &seq[i++]);
            }
            if(line == 2){
                fscanf(parametros, "%f", amin);
            }
            if(line == 3){
                fscanf(parametros, "%f", amax);
            }
            if(line == 4){
                fscanf(parametros, "%f", bmin);
            }
            if(line == 5){
                fscanf(parametros, "%f", bmax);
            }
            if(line == 6){
                fscanf(parametros, "%d", asize);
            }
            if(line == 7){
                fscanf(parametros, "%d", bsize);
            }
            if(line == 8){
                fscanf(parametros, "%d", nmax);
            }
        }
    }
    fclose(parametros);    

What is it that I am doing wrong? Thank you in advance.

Working complete example:

The principal change are in:

  • Loop in line two to read all the values, as I suggest in the comments.
  • Change the variables to values and use &variable in fscanf
  • Eliminate the unnecessary if (with variable line)

Code:

#include <stdio.h>
#include <stdlib.h>

#define MAX_ELEMENT 100
int main(void) {
    FILE* parametros;
    int reading_error = 0;
    int i;
    int seq_length = 0, asize = 0, bsize = 0, nmax = 0;
    float amin = 0, amax = 0, bmin = 0, bmax = 0;
    int seq[MAX_ELEMENT];
    parametros = fopen("E:\\test.txt", "r");
    if (parametros == NULL) {
        printf("\nSe toman valores por defecto.\n");
    } else { // Se asume que el fichero tiene las lineas correctas.
        reading_error += 1 != fscanf(parametros, "%d", &seq_length);
        if (seq_length > MAX_ELEMENT) {
            // handle error
        }
        for (i = 0; i < seq_length;) {
            reading_error = 1 != fscanf(parametros, "%d", &seq[i++]);
        }
        reading_error += 1 != fscanf(parametros, "%f", &amin);
        reading_error += 1 != fscanf(parametros, "%f", &amax);
        reading_error += 1 != fscanf(parametros, "%f", &bmin);
        reading_error += 1 != fscanf(parametros, "%f", &bmax);
        reading_error += 1 != fscanf(parametros, "%d", &asize);
        reading_error += 1 != fscanf(parametros, "%d", &bsize);
        reading_error += 1 != fscanf(parametros, "%d", &nmax);
        fclose(parametros);
    }

    if (!reading_error) {
        printf("%d\n", seq_length);
        for (i = 0; i < seq_length;) {
            printf("%d ", seq[i++]);
        }
        printf("\n");
        printf("%f\n", amin);
        printf("%f\n", amax);
        printf("%f\n", bmin);
        printf("%f\n", bmax);
        printf("%d\n", asize);
        printf("%d\n", bsize);
        printf("%d\n", nmax);
    }

    return 0;
}

Assuming you meant "As you can see, the first figure in the first line is always the amount of elements in NEXT line.", then your data does not reflect that.

Write a function that reads a single set of data by first reading n then looping to read n elements.

Call this function repeatedly until EOF is encountered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM