简体   繁体   English

将文本文件读入2D数组

[英]Read text file into 2D array

So currently I have a working code that is able to read in a text file that contains a 9x9 board each slot in the 2D array is a number between 1-9; 因此,目前我有一个工作代码,该代码能够读取包含9x9电路板的文本文件,二维数组中的每个插槽都是1-9之间的数字; however, some slots have a '-' character mixed into them randomly. 但是,某些插槽中随机混有一个'-'字符。 The only way I found that I could read all the lines successfully and then print them was using a char sudoku[9][9] but this greatly affects other parts of my current assignment. 我发现我可以成功读取所有行然后打印它们的唯一方法是使用char sudoku[9][9]但这极大地影响了我当前作业的其他部分。

The file should just be 9 consecutive rows like so: 该文件应该是连续9行,如下所示:

123-56-89
4561-3--9

etc... 等等...

Could someone show me a way I could successfully scan in the text file contents into a INTEGER 2D array instead of a char one? 有人可以告诉我一种可以成功地将文本文件内容扫描到INTEGER 2D数组而不是char数组中的方法吗?

int main(){
 int i, j;
 char sudoku[9][9];
    char filename[100];

        printf("Please enter file name (W/O .txt): ");
        scanf("%s", filename);

        FILE *file1 = fopen(filename, "r");

        for(i=0;i<9;i++){
            for(j=0;j<9;j++){
                fscanf(file1,"%c\t", &sudoku[i][j]);
            }
        }

        for(i=0;i<9;i++){
            for(j=0;j<9;j++){
                printf("%2c", sudoku[i][j]);
                }
            printf("\n");
        }

fclose(file1);
}
int suduku[9][9];

for (int i = 0; i < 9; i++)
{
    for (int j = 0; j < 9; j++)
    {
        char c;
        if (fscanf(file1, " %c", &c) != 1)
            …report read failure and exit…
        else if (isdigit((unsigned char)c))
            sudoku[i][j] = c - '0';
        else
            sudoku[i][j] = 0;
    }
}

Read the character into a character, then convert to an appropriate integer. 将字符读取为字符,然后转换为适当的整数。 Note that the code won't care if each character from the matrix is separated from the next by a dozen blank lines and there are 20 spaces before the character, or if the whole input is 81 characters on a single line, or any other variation. 请注意,该代码将不在乎矩阵中的每个字符是否与下一字符之间用十几行空白行隔开,并且该字符前是否有20个空格,或者整个输入是否在一行上是81个字符,或其他任何变化形式。 As long as there are at least 81 non-white space characters in the file, it'll be OK. 只要文件中至少有81个非空白字符,就可以了。

#include <stdlib.h>
/* ... */
int sudoku[9][9];
/* ... */
for(i = 0; i < 9; i++) {
    for(j = 0; j < 9; j++) {
        char s[] = "0";
        fscanf(file1, " %c", &s[0]);
        sudoku[i][j] = atoi(s);
    }
}

I prefer to call a standard library function where possible to perform the integer conversion. 我更喜欢在可能的地方调用标准库函数来执行整数转换。 Here, I'm using atoi(). 在这里,我正在使用atoi()。 If it sees a digit, it will return its integer value. 如果看到数字,它将返回其整数值。 If is sees anything else, including the '-' character, it will return 0. If fscanf() runs out of characters, atoi() will also return 0 (since we are initializing s to "0" in every iteration), effectively padding out the rest of the array with '-' characters. 如果看到其他任何东西,包括'-'字符,它将返回0。如果fscanf()用完了字符,atoi()也将返回0(因为我们在每次迭代中都将s初始化为“ 0”)用'-'字符填充数组的其余部分。 (Notice that s is technically a string rather than a character, since that is what atoi() expects as input, but fscanf() will only overwrite its first byte where the character is stored.) (注意,从技术上讲,s是字符串而不是字符,因为那是atoi()期望的输入,但是fscanf()只会覆盖存储字符的第一个字节。)

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

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