将文本文件读入C中的2D数组

时间:2021-09-18 13:26:57

I have a text file of size 8 by 12 (8 rows, 12 columns) (x,y)

我有一个大小为8乘12(8行,12列)(x,y)的文本文件

abcdefghjikl
123456789abc
aerereghjikl
123456789abc
abc43434dfdf
12erere789ab
abcdefghjikl
12345fdfd89a

I'm trying to read each individual character into a 2d array, where the first dimension is the rows, and the second is the columns.

我正在尝试将每个单独的字符读入一个二维数组,其中第一个维度是行,第二个维度是列。

This is what I've tried:

这就是我尝试过的:

int main(void) {
    FILE * fp;
    fp = fopen("test.txt","r");

    char points[8][12];
    int i,j;

    for(i=0; i<8; i++) {
        for(j=0; j<12; j++) {
            fscanf(fp,"%c",&points[i][j]);
        }
    }

    for(i=0; i<8; i++) {
        for(j=0; j<12; j++) {
            printf("%c",points[i][j]);
        }
    }

    return 0;
}

However my output seems to work correctly, up untill the last line of the file.

但是我的输出似乎正常工作,直到文件的最后一行。

abcdefghjikl
123456789abc
aerereghjikl
123456789abc
abc43434dfdf
12erere789ab
abcdefghjikl
12345

Where the last line doesn't fully work. I've tried increasing the array dimensions of the row, which makes the last line appear, but adds garbage values to my output. Any help would be much appreciated.

最后一行没有完全奏效的地方。我已经尝试增加行的数组维度,这会使最后一行显示,但会向我的输出添加垃圾值。任何帮助将非常感激。

3 个解决方案

#1


5  

Newline character, as the name implies, are characters also. You should have found it suspicious that your output preserves newlines.

顾名思义,换行符也是字符。你应该发现你的输出保留换行符是可疑的。

There are in fact 13 characters in each line of the file (assuming a unix system). The twelve printable ones, and a newline.

实际上文件的每一行有13个字符(假设是unix系统)。十二个可打印的,换行。

So for i = 1...7 the first character in each "row" is a newline.

因此,对于i = 1 ... 7,每个“行”中的第一个字符是换行符。

Fortunately, scanf can be instructed to skip whitespace characters when awaiting a character input. Simply add a space in the beginning of the format specifier string:

幸运的是,可以指示scanf在等待字符输入时跳过空格字符。只需在格式说明符字符串的开头添加一个空格:

fscanf(fp, " %c", &points[i][j]);

#2


3  

Note: you are missing 7 chars which is the number of \n put in your array.

注意:您缺少7个字符,这是您放入数组中的\ n数。

abcdefghjikl \n
123456789abc \n
aerereghjikl \n
123456789abc \n
abc43434dfdf \n 
12erere789ab \n 
abcdefghjikl \n
12345

There's a \n at the end of each line. Which is not visible to you but is being stored in array.

每行末尾有一个\ n。哪个对您不可见,但存储在数组中。

You can simply skip this by telling the file pointer position to skip 1 char.

您可以通过告诉文件指针位置跳过1个字符来跳过此操作。

for(i=0; i<8; i++) {
    for(j=0; j<12; j++) {
        fscanf(fp,"%c",&points[i][j]);
    }
    fseek( fp, 1, SEEK_CUR );
}

Output:

输出:

abcdefghjikl
123456789ab
aerereghjik
123456789ab
abc43434dfd
12erere789a
abcdefghjik
12345fdfd89

#3


0  

You should check if return pointer of fp, to ensure the file was opened correctly. Somethin like this is what you need to do:

你应该检查fp的返回指针,以确保文件被正确打开。这样的事情是你需要做的:

FILE * fp;
fp = fopen("test.txt","r");
if (fp == NULL) {
    /* handle exit */

As for you error concerning the last line of the file, you need to change this segement:

至于有关文件最后一行的错误,您需要更改此段:

for(i=0; i<8; i++) {
    for(j=0; j<12; j++) {
        fscanf(fp,"%c",&points[i][j]);
    }
}

To:

至:

for(i=0; i<8; i++) {
    for(j=0; j<12; j++) {
        if (fscanf(fp,"%c ",&points[i][j]) != 1) {
            fprintf(stderr, "Non character found.\n");
            return 1;
        }
    }
    printf("\n");
}

Which skips any number of trailing white space from fscanf(). fscanf() will leave a newline character in the input buffer, and gets carried over on the next fscanf() call. Its also safe to check that 1 character was read.

从fscanf()跳过任意数量的尾随空格。 fscanf()将在输入缓冲区中留下换行符,并在下一个fscanf()调用中继承。检查已读取1个字符也是安全的。

#1


5  

Newline character, as the name implies, are characters also. You should have found it suspicious that your output preserves newlines.

顾名思义,换行符也是字符。你应该发现你的输出保留换行符是可疑的。

There are in fact 13 characters in each line of the file (assuming a unix system). The twelve printable ones, and a newline.

实际上文件的每一行有13个字符(假设是unix系统)。十二个可打印的,换行。

So for i = 1...7 the first character in each "row" is a newline.

因此,对于i = 1 ... 7,每个“行”中的第一个字符是换行符。

Fortunately, scanf can be instructed to skip whitespace characters when awaiting a character input. Simply add a space in the beginning of the format specifier string:

幸运的是,可以指示scanf在等待字符输入时跳过空格字符。只需在格式说明符字符串的开头添加一个空格:

fscanf(fp, " %c", &points[i][j]);

#2


3  

Note: you are missing 7 chars which is the number of \n put in your array.

注意:您缺少7个字符,这是您放入数组中的\ n数。

abcdefghjikl \n
123456789abc \n
aerereghjikl \n
123456789abc \n
abc43434dfdf \n 
12erere789ab \n 
abcdefghjikl \n
12345

There's a \n at the end of each line. Which is not visible to you but is being stored in array.

每行末尾有一个\ n。哪个对您不可见,但存储在数组中。

You can simply skip this by telling the file pointer position to skip 1 char.

您可以通过告诉文件指针位置跳过1个字符来跳过此操作。

for(i=0; i<8; i++) {
    for(j=0; j<12; j++) {
        fscanf(fp,"%c",&points[i][j]);
    }
    fseek( fp, 1, SEEK_CUR );
}

Output:

输出:

abcdefghjikl
123456789ab
aerereghjik
123456789ab
abc43434dfd
12erere789a
abcdefghjik
12345fdfd89

#3


0  

You should check if return pointer of fp, to ensure the file was opened correctly. Somethin like this is what you need to do:

你应该检查fp的返回指针,以确保文件被正确打开。这样的事情是你需要做的:

FILE * fp;
fp = fopen("test.txt","r");
if (fp == NULL) {
    /* handle exit */

As for you error concerning the last line of the file, you need to change this segement:

至于有关文件最后一行的错误,您需要更改此段:

for(i=0; i<8; i++) {
    for(j=0; j<12; j++) {
        fscanf(fp,"%c",&points[i][j]);
    }
}

To:

至:

for(i=0; i<8; i++) {
    for(j=0; j<12; j++) {
        if (fscanf(fp,"%c ",&points[i][j]) != 1) {
            fprintf(stderr, "Non character found.\n");
            return 1;
        }
    }
    printf("\n");
}

Which skips any number of trailing white space from fscanf(). fscanf() will leave a newline character in the input buffer, and gets carried over on the next fscanf() call. Its also safe to check that 1 character was read.

从fscanf()跳过任意数量的尾随空格。 fscanf()将在输入缓冲区中留下换行符,并在下一个fscanf()调用中继承。检查已读取1个字符也是安全的。