C语言 将文本文件中的数字读入 C 中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20378430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:26:45  来源:igfitidea点击:

Reading numbers from a text file into an array in C

carraysfilescanf

提问by Vonti

I'm a programming noob so please bear with me.

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

I'm trying to read numbers from a text file into an array. The text file, "somenumbers.txt" simply holds 16 numbers as so "5623125698541159".

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


}

The program doesn't work. It compiles but outputs:

该程序不起作用。它编译但输出:

Number is: -104204697

号码是:-104204697

Number is: 0

数字是:0

Number is: 4200704

号码是:4200704

Number is: 2686672

号码是:2686672

Number is: 2686728

号码是:2686728

Number is: 2686916

号码是:2686916

Number is: 2004716757

号码为:2004716757

Number is: 1321049414

号码是:1321049414

Number is: -2

数字是:-2

Number is: 2004619618

号码为:2004619618

Number is: 2004966340

号码为:2004966340

Number is: 4200704

号码是:4200704

Number is: 2686868

号码是:2686868

Number is: 4200798

号码是:4200798

Number is: 4200704

号码是:4200704

Number is: 8727656

号码是:8727656

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

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

回答by BLUEPIXY

change to

改成

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

回答by haccks

5623125698541159is treated as a single number (out of range of inton most architecture). You need to write numbers in your file as

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

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

for 16 numbers.

为 16 个号码。

If your file has input

如果您的文件有输入

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

then change %dspecifier in your fscanfto %d,.

然后将%d您的说明符更改fscanf%d,.

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

Here is your full code after few modifications:

这是经过一些修改后的完整代码:

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

回答by Fiddling Bits

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

This is attempting to read the whole string, "5623125698541159"into &numArray[0]. You need spaces between the numbers:

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

5 6 2 3 ...

回答by Pierre Arlaud

Loop with %c to read the stream character by character instead of %d.

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

回答by Loic

There are two problems in your code:

您的代码中有两个问题:

  • the return value of scanfmustbe checked
  • the %dconversion does not take overflows into account (blindly applying *10 + newdigitfor each consecutive numeric character)
  • scanf必须检查的返回值
  • %d转换不会采取溢出考虑(盲目地施加*10 + newdigit于每个连续的数字字符)

The first value you got (-104204697) is equals to 5623125698541159modulo 2^32; it is thus the result of an overflow (if intwhere 64 bits wide, no overflow would happen). The next values are uninitialized (garbage from the stack) and thus unpredictable.

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

The code you need could be (similar to the answer of BLUEPIXY above, with the illustration how to check the return value of scanf, the number of items successfully matched):

您需要的代码可能是(类似于上面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;
}