繁体   English   中英

在C中将字符作为整数添加到数组中

[英]Adding characters to an array as integers in C

我试图将整数添加到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;
}

的test.txt

731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511
125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749
303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243
525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474
821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042
242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606
0588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

有人指出我正确的方向将是伟大的。

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

int num[count]

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

你也不能将一个char传递给atoi,你必须传递一个2元素的char数组,第二个元素是0,或者你可以使用

num[j] = temp - '0';

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

希望这有助于您使其工作。

在添加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

你做完了!

以下应用程序工作并提供以下输出(在包含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){你使用临时计数器来索引元素

希望这可以帮助

暂无
暂无

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

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