在C语言中从文本文件读取数字到数组中

40

我是一个编程新手,请多多包涵。

我正在尝试将文本文件中的数字读入数组。文本文件名为"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]);
    }


}
程序无法运行。虽然编译通过,但输出如下:

Number is: -104204697

Number is: 0

Number is: 4200704

Number is: 2686672

Number is: 2686728

Number is: 2686916

Number is: 2004716757

Number is: 1321049414

Number is: -2

Number is: 2004619618

Number is: 2004966340

Number is: 4200704

Number is: 2686868

Number is: 4200798

Number is: 4200704

Number is: 8727656

Process returned 20 (0x14) execution time : 0.118 s Press any key to continue.


43
您已经到达...一个stackoverflow。真实的故事。 - Pierre Arlaud
6个回答

42

改为

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

@chux 如果减号被附加,那么它是无用的,因为已经确定它是最大字段宽度之一。 - BLUEPIXY
3
谢谢,这很有效,但是你能解释一下 %1d 中的数字 1 到底发生了什么吗?例如,%2d 不起作用。另外,我有另一个文件,其中有 16 个数字,但每个数字之间都用逗号分隔。我以为 fscanf(myFile, "%d,", &numberArray[i]); 可以工作,但并没有。你能帮忙吗? - Vonti
2
@Vonti "%1d" 1: 读取字段的最大大小。如果您使用逗号分隔,则可能是"%d," - BLUEPIXY
这是我所拥有的,但它不起作用。我将打开另一个帖子,并发布我的代码。这里没有太多的空间。 - Vonti

25

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,", &numberArray[i]); - Vonti
@Vonti,我在一些修改后发布了您的完整代码。请查看编辑。 - haccks
谢谢,这个很好用。我在我的程序的第一部分之后关闭文件时遇到了一些问题。我运行 fclose(myFile); 然后在下一行运行 if (myFile == NULL) { printf("Error Reading File\n\n"); exit (0); } else { printf("file still ok\n\n"); } 但它说 else 或者 "file still ok",这真的很奇怪,但我设法用 { } 关闭(隐藏)程序的两个部分,以使其正常工作。 - Vonti
应该是的。下次文件再次通过fopen打开。 - haccks
1
这个问题与我的实际问题无关,但是你的答案直接回答了我遇到的问题...更不用说你还解释了为什么会出现这个错误。谢谢。+1。 - rayryeng
@rayryeng;我很高兴它有帮助 :) - haccks

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

这是尝试将整个字符串"5623125698541159"读入&numArray[0]。你需要在数字之间加上空格:

5 6 2 3 ...

2

你的代码存在两个问题:

  • 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;
}

2
使用%c循环读取流中的每个字符,而不是%d。

0

请按以下方式输入您的文件输入: 例如: 12 13 22 45 (每输入一个数字后按回车键) 然后运行您的程序,它将正常运行


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接