【问题标题】:Reading numbers from a text file into an array in C将文本文件中的数字读入C中的数组
【发布时间】:2013-12-21 02:53:21
【问题描述】:

我是一个编程菜鸟,所以请多多包涵。

我正在尝试将文本文件中的数字读入数组。文本文件“somenumbers.txt”仅包含 16 个数字,如“5623125698541159”。

#include <stdio.h>
main()
{

    FILE *myFile;
    myFile = fopen("somenumbers.txt", "r");

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

    for (i = 0; i < 16; i++)
    {
        fscanf(myFile, "%d", &numberArray[i]);
    }

    for (i = 0; i < 16; i++)
    {
        printf("Number is: %d\n\n", numberArray[i]);
    }


}

程序不工作。它编译但输出:

号码是:-104204697

数字是:0

号码是:4200704

号码是:2686672

号码是:2686728

号码是:2686916

号码是:2004716757

号码是:1321049414

数字是:-2

号码是:2004619618

号码是:2004966340

号码是:4200704

号码是:2686868

号码是:4200798

号码是:4200704

号码是:8727656

进程返回 20 (0x14) 执行时间:0.118 s 按任意键继续。

【问题讨论】:

  • 你已经达到了……一个stackoverflow。真实的故事。

标签: c arrays file scanf


【解决方案1】:

改成

fscanf(myFile, "%1d", &numberArray[i]);

【讨论】:

  • @chux 附加减号是没用的,因为它一直是要读取的最大字段宽度之一。
  • 谢谢,这行得通,但你能解释一下%1d 中的 1 到底发生了什么吗?例如,%2d 不起作用。另外,我有另一个文件,它有 16 个数字,但每个数字用逗号分隔。我认为fscanf(myFile, "%d,", &amp;numberArray[i]); 会起作用,但它不会。你能帮忙吗?
  • @Vonti "%1d" 1:读取文件的最大大小。如果用逗号分隔,我认为可能是"%d,"
  • 这是我拥有的,但它不起作用。我将用我的代码打开另一个问题。这里没有那么多空间。
【解决方案2】:

5623125698541159 被视为单个数字(在大多数架构上超出int 的范围)。您需要在文件中写入数字为

5 6 2 3 1 2 5  6 9 8 5 4 1 1 5 9  

16 个数字。

如果你的文件有输入

5,6,2,3,1,2,5,6,9,8,5,4,1,1,5,9 

然后将fscanf 中的%d 说明符更改为%d,

  fscanf(myFile, "%d,", &numberArray[i] );  

这是经过少量修改后的完整代码:

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

int main(){

    FILE *myFile;
    myFile = fopen("somenumbers.txt", "r");

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

    if (myFile == NULL){
        printf("Error Reading File\n");
        exit (0);
    }

    for (i = 0; i < 16; i++){
        fscanf(myFile, "%d,", &numberArray[i] );
    }

    for (i = 0; i < 16; i++){
        printf("Number is: %d\n\n", numberArray[i]);
    }

    fclose(myFile);

    return 0;
}

【讨论】:

  • 我的其他文件格式为 1,2,3,4 等,但没有空格。但是将逗号添加到 %d 仍然不起作用。fscanf(myFile, "%d,", &amp;numberArray[i]);
  • @Vonti,经过一些修改,我将发布您的完整代码。查看编辑。
  • 谢谢这很好用。在我的程序的第一部分之后,我在使用 fclose 关闭文件时遇到了一些问题。我运行 fclose(myFile); 然后在下一行运行 if (myFile == NULL) { printf("Error Reading File\n\n"); exit (0); } else { printf("file still ok\n\n"); } 但它说 else 或“文件仍然可以”,这真的很奇怪,但我设法用 { } 关闭(彼此隐藏)程序的两半让它工作。
  • 是的。它应该是。下次通过fopen再次打开文件。
  • 这个问题与我的实际问题无关,但你的回答直接回答了我遇到的问题......更不用说你实际上解释了为什么错误正在发生。谢谢你。 +1。
【解决方案3】:
for (i = 0; i < 16; i++)
{
    fscanf(myFile, "%d", &numberArray[i]);
}

这是试图将整个字符串 "5623125698541159" 读入 &amp;numArray[0]。数字之间需要空格:

5 6 2 3 ...

【讨论】:

    【解决方案4】:

    使用 %c 循环以逐个字符而不是 %d 读取流。

    【讨论】:

      【解决方案5】:

      你的代码有两个问题:

      • scanf的返回值必须勾选
      • %d 转换不考虑溢出(对每个连续的数字字符盲目地应用 *10 + newdigit

      你得到的第一个值(-104204697)等于56231256985411592^32;因此它是溢出的结果(如果int 64 位宽,则不会发生溢出)。下一个值未初始化(堆栈中的垃圾),因此无法预测。

      你需要的代码可以是(类似于上面BLUEPIXY的回答,并说明如何检查scanf的返回值,成功匹配的项目数):

      #include <stdio.h>
      
      int main(int argc, char *argv[]) {
          int i, j;
          short unsigned digitArray[16];
          i = 0;
          while (
              i != sizeof(digitArray) / sizeof(digitArray[0])
           && 1 == scanf("%1hu", digitArray + i)
          ) {
              i++;
          }
          for (j = 0; j != i; j++) {
              printf("%hu\n", digitArray[j]);
          }
          return 0;
      }
      

      【讨论】:

        【解决方案6】:

        像这样输入你的文件输入 前任: 12 13 22 45 (每个数字按回车后) 然后运行你的程序它会正常运行

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-01-20
          • 2010-09-29
          • 2010-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多