0

我正在尝试将整数添加到 C 中的数组中。但是我正在从文件中读取字符,因此我必须先将它们转换为整数。出于某种原因,我的程序在停止工作之前甚至无法启动。我认为这是一个转换问题,但我是 C 新手。

新的编辑代码:

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

int main (void)
{
    FILE* fp;
    const char filename[] = "test.txt";
    char ch;
    int num[1000];
    int j = 0;
    char temp;

    fp = fopen(filename, "r");
    if( fp == NULL )
    {
        printf( "Cannot open file: %s\n", filename);
        exit(8);
    }

    while(!feof(fp))
    {
        temp = fgetc(fp);
        num[j] = temp - '0';
        j++;
    }
    printf("First in the array is: %d,  last is; %d", num[0], num[999]);


    fclose(fp);

    return EXIT_SUCCESS;
}

测试.txt

731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511
125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749
303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243
525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474
821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042
242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606
0588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

有人指出我正确的方向会很棒。

4

3 回答 3

1

你不能像这样在 C 中动态分配一个 int 数组,所以

int num[count]

不管用。使用 malloc 使用最大值或动态分配。

你也不能将单个字符传递给 atoi,你必须传递一个 2 元素字符数组,第二个元素为 0,或者你可以只使用

num[j] = temp - '0';

在第二个 while 循环中检查 fp 的 EOF 也是错误的,使用feof(fp)temp != EOF

希望这可以帮助您使其正常工作。

于 2012-03-06T12:17:42.317 回答
1

在您添加test.txt.

代替

while(!feof(fp))
{
    temp = fgetc(fp);
    num[j] = temp - '0';
    j++;
}

改成

while(!feof(fp)&& (j<1000))
{
    temp = fgetc(fp);
    if ( temp != EOF && ( temp>='0' && temp<='9') ) // To filter out non-numbers and also the last EOF
       num[j++] = temp - '0';

}

而不是

printf("First in the array is: %d,  last is; %d", num[0], num[999]);

改成

printf("First in the array is: %d,  last is; %d", num[0], num[j-1]); // j-1 so that it works even when there are less than 1000 numbers and to keep it generic

你完成了!

于 2012-03-06T13:04:49.283 回答
0

以下应用程序有效并提供以下输出(在包含 1 3 5 5565 的文本文件中)在此处输入图像描述

int main (void)
{
    FILE* fp;
    const char filename[10] = "test.txt";
    char ch;
    int count = 0;
    int num[100];


    fp = fopen(filename, "r");
    if( fp == NULL )
    {
        printf( "Cannot open file: %s\n", filename);
        exit(8);
         } 

    while (fscanf(fp, "%d", &num[count]) !=EOF){
          count++;
         }

    rewind(fp);

    printf("Text file contains: %d\n", count);
    printf("First in the array is: %d,  last is; %d\n", num[0], num[count-1]);


    fclose(fp);
    system("pause");
    return EXIT_SUCCESS;
}

如果您希望数组动态调整大小,则需要在添加元素之前循环遍历元素并计算它们,就像您在主代码中的示例中所做的那样。而不是while (fscanf(fp, "%d", &num[count]) !=EOF){你使用临时计数器来索引元素

希望这可以帮助

于 2012-03-06T12:38:00.330 回答