繁体   English   中英

将文件中的数字读入数组

[英]Reading numbers from a file into an array

我想使用fgetc()命令从文件中读取3位数字,中间带有空格,并将它们放入当前不起作用的数组中,因为生成的数组中的对象完全不同。 我究竟做错了什么? (我使用其中带有“ 107 313 052 614”的文件,结果输出为“ 5435 5641 5380 5942”)

我的代码:

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


void print_array(int* arrayp, int lengthp){
    int i;
    for(i=0;i<lengthp;i++){
        printf("%i ", arrayp[i]);
    }
    return;
}

int main(){
    int length=1;
    int i;
    FILE *fp1;
    fp1 = fopen("durations.txt", "r");
    fgetc(fp1);fgetc(fp1);fgetc(fp1);
    while(fgetc(fp1)!=EOF){
        length++;
        fgetc(fp1);
        fgetc(fp1);
        fgetc(fp1);
    }
    fclose(fp1);
    int* list = (int*) malloc(sizeof(int)*length);
    FILE *fp2;
    fp2 = fopen("durations.txt", "r");
    for(i=0;i<length;i++){
        list[i]=0;
        list[i]+=100*(fgetc(fp2));
        list[i]+=10*(fgetc(fp2));
        list[i]+=(fgetc(fp2));
        fgetc(fp2);
    }
    fclose(fp2);
    print_array(list, length);
    return 0;
}

用于在“可读”文件中存储数字的字符不是“数字”。 最受欢迎的编码是ascii字符编码 ,例如。 1位数字用十进制数字49表示。

因为ascii编码中的0、1、2 ... 9位数字是按递增顺序编码的,所以您只需减去48(即'0'字符)就可以将数字字符转换为机器格式Just - '0'

将您更改为:

for(i=0;i<length;i++){
    list[i]=0;
    list[i]+=100*(fgetc(fp2) - '0');
    list[i]+=10*(fgetc(fp2) - '0');
    list[i]+=(fgetc(fp2) - '0');
    fgetc(fp2);
}

这也说明了程序的当前输出。 如果您不从数字中减去'0' ,那么例如对于107您将得到:

100 * '1' + 10 * '0' + '7' =
100 * 49  + 10 * 48  + 55  =
5435

494855是用于数字的十进制值107在ASCII表。

问题是您正在读取每个数字的(可能是) ASCII值,并假设这是该数字的值。 您需要从每个值中减去零字符的值,如下所示:

for (i = 0; i < length; i++) {
    list[i] = 0;
    list[i] += 100 * (fgetc(fp2)-'0');
    list[i] += 10 * (fgetc(fp2)-'0');
    list[i] += (fgetc(fp2)-'0');
    fgetc(fp2);
}

即使您的系统不使用ASCII编码,这也将起作用。

将数字读入cstrings然后使用stdlib函数atoi将每个字符串转换为数字,然后再加载到ints数组中可能会更简单:

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

int main(void) {

    //open the file for reading with error checking:
    FILE* fp;
    char c = '0';
    int length = 0;
    fp = fopen("durations.txt", "r");
    if (fp == NULL) {
        printf("Could not open file\n");
        return 0;
    }

    //count the number of lines in a EOF-terminated string w/o newlines
    for (c = fgetc(fp); c != EOF; c = fgetc(fp)) {
            if (c == ' ') {
                length += 1;
            }
        }
    rewind(fp);    //go back to file end w/o needing close/open 

    //Then, assuming only spaces are between numbers (so length = length + 1)
    char buffer[4];
    int* lst = (int*) malloc(sizeof(int)*length);
    for (int i = 0; i < length + 1; i++) {
        fscanf(fp, "%s", buffer);  //stops at spaces
        lst[i] = atoi(buffer);
        }

    //close the file with error checking
    int check = fclose(fp);
    if (check != 0) {
        printf("File close failed");
        return 0;
    }
    for (int i = 0; i < length + 1; i++) {   //print result
        printf("%d\n", lst[i]);
    }

    free(lst);       //clean up memory after use
    lst = NULL;

    return 0;
}

暂无
暂无

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

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