简体   繁体   English

如何通过 integer 读取文件 integer 并将它们放入二维数组中?

[英]How can I read a file integer by integer and put them into a 2D array?

I am trying to read all the numbers from a txt file and put them into a 2D array.我正在尝试从 txt 文件中读取所有数字并将它们放入二维数组中。 I shouldn't worry about the size and stuff because I know it will be entered in 9 rows and in each row there will be 9 numbers.我不应该担心大小和东西,因为我知道它将输入 9 行,每行将有 9 个数字。 But if I run this code I get the following output.但如果我运行这段代码,我会得到以下 output。

int main() {
    FILE *fpointer = fopen("filename.txt", "r");
    int ch;
    int arr[9][9];
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            ch = fgetc(fpointer);
            arr[i][j] = ch;
            //printf("%d", ch);
        }
    }
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            printf("%d  ", arr[i][j]);
        }
        printf("\n");
    }
    fclose(fpointer);
    return 0;
}

Output: Output:

49  51  52  53  54  55  48  57  50
10  52  50  49  57  56  51  55  52
49  10  51  49  50  52  57  56  55
49  51  10  52  50  51  53  49  51
53  49  49  10  50  51  52  54  51
53  55  50  49  10  53  50  51  54
55  56  50  52  53  10  54  52  54
53  56  57  51  50  49  10  53  52
57  50  57  56  51  53  54  10  50

But the entered numbers are:但输入的数字是:

134567092
421983741
312498713
423513511
234635721
523678245
646589321
549298356
234698721

I assume it maybe have to do something with the fgets() function, but I tried to use getw() , but then I get even worse numbers.我认为它可能必须对fgets() function 做一些事情,但我尝试使用getw() ,但后来我得到了更糟糕的数字。 Maybe it tries to read the file in hexadecimals or something.也许它会尝试以十六进制或其他方式读取文件。 Any ideas?有任何想法吗?

I'm not totally sure what behavior you were hoping for here, but I recommend decoding the ASCII digits before you store the numbers in your array, like this:我不完全确定您希望在这里得到什么行为,但我建议您在将数字存储到数组中之前解码 ASCII 数字,如下所示:

arr[i][j] = ch - '0';

Also, note that fgetc will return line-ending bytes like 10, because those are characters in your file too, even if they are not exactly visible.另外,请注意fgetc将返回像 10 这样的行尾字节,因为这些也是文件中的字符,即使它们不完全可见。 So when you receive one of those, you need to discard it and call fgetc again.因此,当您收到其中一个时,您需要将其丢弃并再次调用fgetc Or you could insert an extra fgetc call at the end of each line (right after your j loop) to read those.或者您可以在每行的末尾(就在您的j循环之后)插入一个额外的fgetc调用来读取它们。

Looks like you need to convert the char from an ascii representation of the digit, into the actual digit.看起来您需要将字符从数字的 ascii 表示形式转换为实际数字。 See also: Convert a character digit to the corresponding integer in C另请参阅: 将字符数字转换为 C 中对应的 integer

So this part:所以这部分:

    for(int j =0; j<9;j++){
        ch = fgetc(fpointer);
        arr[i][j] = ch;
        //printf("%d", ch);
    }

Would become this:会变成这样:

    for(int j =0; j<9;j++){
        ch = fgetc(fpointer);
        if(isdigit(ch)) {
            ch = ch - '0';
        } else {
            printf("error: Non-digit found!\n");
            exit(1);
        }
        arr[i][j] = ch;
        //printf("%d", ch);
    }

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

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