如何从文件中读取数据到C中的二维数组? [英] How to read data from file into two dimension array in C?

查看:57
本文介绍了如何从文件中读取数据到C中的二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取文件并打印出文件中的内容.文件中的数据如下所示,由100行和10列组成.

I'm trying to read from a file and print out what's in the file. Data in file is something like the following and consist of 100 rows and 10 columns.

-1,0.53,1,1,1,0,0.8,1,0.5,0

这是我尝试过的:

    #include <stdio.h>
    #include <stdlib.h>
    #define NUMBEROFINPUT 100 //100 inputs.
    #define NUMBEROFCOLUMN 10 //10 rows.

    int main(){
        int *dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];

        FILE *dataFileptr;
        dataFileptr = fopen("group5_8.txt", "r");
        if (dataFileptr == NULL){
           perror("Error");
           return 1;
        }

        for(int i = 0; i < NUMBEROFINPUT; ++i){
            for(int j = 0; j < NUMBEROFCOLUMN; ++j){
               fscanf(dataFileptr, " %f", &dataFileInput[i][j]);
               printf("a[%d][%d] = %.2f\n", i+1,j+1, dataFileInput[i][j]);
            }
        }
    }

但是,当我打印出结果时,我只会得到 0.00 .我还是C语言编程的新手.我在做什么错了?

However, I'm only getting 0.00 when I print out the result. I'm still rather new to C programming. What am I doing wrong?

推荐答案

您应更改以下行:

int *dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];

如下:

float dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];

或者,您可以将数组声明为 double 并使用%lf

Alternatively, you can declare the array as double and read using %lf

我还必须跳过逗号,如下所示:

I also had to skip the commas as follows:

#include <stdio.h>
#include <stdlib.h>
#define NUMBEROFINPUT 100 //100 inputs.
#define NUMBEROFCOLUMN 10 //10 rows.

int main(){
    float dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];
    
    FILE *dataFileptr;
    dataFileptr = fopen("group5_8.txt", "r");
    if (dataFileptr == NULL){
       perror("Error");
       return 1;
    }

    for(int i = 0; i < NUMBEROFINPUT; ++i){
        for(int j = 0; j < NUMBEROFCOLUMN; ++j){
           fscanf(dataFileptr, "%f", &dataFileInput[i][j]);
           fgetc(dataFileptr);
           printf("a[%d][%d] = %.2f\n", i+1,j+1, dataFileInput[i][j]);
        }
    }
    
    fclose(dataFileptr);
}

注意对 fgetc 的调用.

为了完整起见,我想请您注意,即使使用int *数组,读取也是成功的.打印失败,因为数据被解释为int *,然后转换为浮点数.要解释存储在int *数组元素中的字节,请首先将该元素的地址转换为要浮点的指针,然后获取存储在该地址的浮点值.相同的代码,除了打印语句.

For the sake of completion, I would like you to note that the reading is successful even when using an int* array. The printing fails because the data is interpreted as an int* and then converted to a float. To interpret the bytes stored in the int* array element, first cast the address of the element to a pointer to float, then get the value of the float stored at this address. Same code except for the print statement.

#include <stdio.h>
#include <stdlib.h>
#define NUMBEROFINPUT 100 //100 inputs.
#define NUMBEROFCOLUMN 10 //10 rows.

int main(){
    int *dataFileInput[NUMBEROFINPUT][NUMBEROFCOLUMN];
    
    FILE *dataFileptr;
    dataFileptr = fopen("group5_8.txt", "r");
    if (dataFileptr == NULL){
       perror("Error");
       return 1;
    }

    for(int i = 0; i < NUMBEROFINPUT; ++i){
        for(int j = 0; j < NUMBEROFCOLUMN; ++j){
           fscanf(dataFileptr, "%f", &dataFileInput[i][j]);
           fgetc(dataFileptr);
           printf("a[%d][%d] = %.2f\n", i+1,j+1, *((float *) &dataFileInput[i][j]));
        }
    }
    
    fclose(dataFileptr);
}

这篇关于如何从文件中读取数据到C中的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆