简体   繁体   English

用C将文本文件读入2D数组

[英]Reading in a text file into a 2D array in C

I have a text file of size 8 by 12 (8 rows, 12 columns) (x,y) 我有一个文本文件,大小为8 x 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. 我正在尝试将每个字符读入2d数组,其中第一个维度是行,第二个维度是列。

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. 任何帮助将非常感激。

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). 实际上,文件的每一行中都有13个字符(假设使用Unix系统)。 The twelve printable ones, and a newline. 十二个可打印的字符和一个换行符。

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. 幸运的是,可以在等待字符输入时指示scanf跳过空白字符。 Simply add a space in the beginning of the format specifier string: 只需在格式说明符字符串的开头添加一个空格:

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

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. 每行末尾都有一个\\n Which is not visible to you but is being stored in array. 这对您不可见,但存储在数组中。

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

You should check if return pointer of fp , to ensure the file was opened correctly. 您应该检查fp返回指针,以确保文件已正确打开。 Somethin like this is what you need to do: 这样的事情是您需要做的:

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()任何数量的尾随空白。 fscanf() will leave a newline character in the input buffer, and gets carried over on the next fscanf() call. fscanf()将在输入缓冲区中保留换行符,并在下一个fscanf()调用中继续执行。 Its also safe to check that 1 character was read. 检查已读取1字符也很安全。

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

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