简体   繁体   English

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

[英]Reading numbers from a text file into an array in C

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按任意键继续。

改成

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

5623125698541159 is treated as a single number (out of range of int on most architecture). 5623125698541159被视为一个数字(在大多数体系结构中int超出范围)。 You need to write numbers in your file as 您需要在文件中写数字

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 %d specifier in your fscanf to %d, . 然后将fscanf %d说明符更改为%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;
}
for (i = 0; i < 16; i++)
{
    fscanf(myFile, "%d", &numberArray[i]);
}

This is attempting to read the whole string, "5623125698541159" into &numArray[0] . 这试图将整个字符串"5623125698541159"读入&numArray[0] You need spaces between the numbers: 您需要在数字之间添加空格:

5 6 2 3 ...

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

There are two problems in your code: 您的代码中有两个问题:

  • the return value of scanf must be checked 必须检查scanf的返回值
  • the %d conversion does not take overflows into account (blindly applying *10 + newdigit for each consecutive numeric character) %d转换不考虑溢出(对于每个连续的数字字符,盲目应用*10 + newdigit

The first value you got ( -104204697 ) is equals to 5623125698541159 modulo 2^32 ; 您获得的第一个值( -104204697 )等于56231256985411592^32 ; it is thus the result of an overflow (if int where 64 bits wide, no overflow would happen). 因此,它是溢出的结果(如果int是64位宽,则不会发生溢出)。 The next values are uninitialized (garbage from the stack) and thus unpredictable. 下一个值是未初始化的(来自堆栈的垃圾),因此是不可预测的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM