简体   繁体   English

在C中从文件读取到数组

[英]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 将变量更改为值,并在fscanf使用&variable
  • Eliminate the unnecessary if (with variable line) 消除不必要的if(使用可变行)

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. 假设您的意思是“如您所见,第一行中的第一个数字始终是NEXT行中的元素数量。”,那么您的数据将不反映这一点。

Write a function that reads a single set of data by first reading n then looping to read n elements. 通过首先读取n然后循环读取n个元素来编写一个读取一组数据的函数。

Call this function repeatedly until EOF is encountered. 重复调用此函数,直到遇到EOF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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