【问题标题】:Reading a list of numbers from text file into 2d array将文本文件中的数字列表读入二维数组
【发布时间】:2016-12-05 04:23:23
【问题描述】:

我正在尝试将文本文件中的数字读入数组。文本文件“snumbers.txt”有 5 行,每行 10 个数字,所有数字都用空格分隔:

11 10 23 3 23 98 39 12 9 10
10 23 23 23 23 2 2 2 2 2
…etc…

我的代码:

#include <stdio.h>
int main()
{
    FILE *myFile;
    myFile = fopen("snumbers.txt", "r");

    //read file into array
    int numberArray[100][100];
    int i;



}

如果我访问numberArray[1][1],我该怎么做,它会给我数字“23”。

【问题讨论】:

  • 使用两个for 循环,第二个在第一个下测试。
  • 如果您确定输入数据使用循环相应地读取到数组。 for(){for(){}}
  • 请注意,您的示例输入数据似乎与您的 5x20 声明不相关。您显示两行数字,每行有 10 个数字。真正的数据格式是什么?使用fscanf()读取数字有什么问题?
  • @JonathanLeffler 我说过等等......数字并不重要,这就是没有“”的格式
  • 文件的格式有所不同。特别是,您是否费心检查是否只有 5 行并且每行有 20 个数字,或者您最终是否检查文件和布局中至少有 5 x 20 = 100 个数字,这会有所不同根本不重要,或者您是否坚持在文件中恰好有 100 个数字。有很多方法可以做到这一点。使用fscanf() 意味着你放弃了线路检查;使用fgets() 然后sscanf() 意味着您可以检查每行的正确数量。如果没有您提供的准确信息,我们将无法为您提供很好的帮助。

标签: c arrays multidimensional-array fopen


【解决方案1】:

这里是将您的输入文件读入数组的完整代码。我创建了一个“转换器”来读取字符并将它们转换为数字。假设您在数字之间有一个空格。您可以按原样使用它或从中学习,以便使用以下示例实现一些解决方案:

#include <stdio.h>
#include <string.h>

int main(void){
  FILE *myFile; // file pointer to read the input file
  int   i,      // helper var , use in the for loop
        n,      // helper var, use to read chars and create numbers
        arr[100][100] , // the 'big' array
        row=0,  // rows in the array
        col,    // columns in the array
        zero=(int)'0';  // helper when converting chars to integers
  char line[512]; // long enough to pickup one line in the input/text file
  myFile=fopen("snumbers.txt","r");
  if (myFile){
        // input file have to have to format of number follow by one not-digit char (like space or comma)
        while(fgets(line,512,myFile)){
                col = 0;        // reset column counter for each row
                n = 0;          // will help in converting digits into decimal number
                for(i=0;i<strlen(line);i++){
                        if(line[i]>='0' && line[i]<='9') n=10*n + (line[i]-zero);
                        else {
                                arr[row][col++]=n;
                                n=0;
                        }
                }
                row++;
        }
        fclose(myFile);

        // we now have a row x col array of integers in the var: arr
        printf("arr[1][1] = %d\n", arr[1][1]); // <-- 23
        printf("arr[0][9] = %d\n", arr[0][9]); // <-- 10
        printf("arr[1][5] = %d\n", arr[1][5]); // <-- 2

  } else {
        printf("Error: unable to open snumbers.txt\n");
        return 1;
  }
}

【讨论】:

  • 我不相信变量zero 有帮助,尤其是在范围检查不使用它的情况下。
  • 变量 0 将保存 48 的值(数字 0 的 ascii),没有它,“n=10*n + line[i]”将不起作用,因为您将添加 48 “0”、49 表示“1”等
  • 我会使用'0' 而不是变量(甚至不是const int)。演员表是不必要的;字符文字是 int 值。如果您使用if (line[i] &gt;= zero &amp;&amp; line[i] &lt;= nine)(具有适当的定义:const int nine = '9';),那么也许我不会费心发表评论。但是,我认为if (line[i] &gt;= '0' &amp;&amp; line[i] &lt;= '9') 是清晰的,并且转换算法可以是n = 10 * n + line[i] - '0';,而不会失去清晰度——实际上,清晰度会略有提高。
猜你喜欢
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
相关资源
最近更新 更多