简体   繁体   中英

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)

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.

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.

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

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:

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

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

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.

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

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. 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() 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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