0

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.

3
  • 4
    There's a \n at each line which your putting in your array as well. Commented Feb 15, 2017 at 15:41
  • 1
    Check the return value of fopen(), fscanf()... Commented Feb 15, 2017 at 15:42
  • You can skip the \n using fseek at the end of the execution of the inner loop. Commented Feb 15, 2017 at 16:02

4 Answers 4

7

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]);
Sign up to request clarification or add additional context in comments.

2 Comments

So using strtok() on the array right after the scanf to remove the newline character, yields the same output.
@erefeofkeo - strtok is a sledgehammer. This is a job for a screw-driver
3

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

Comments

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:

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.

Comments

-1

take your datas in to the txt file like this (without \n):

abcdefghjikl123456789abcaerereghjikl123456789abcabc43434dfdf12erere789ababcdefghjikl12345fdfd89a

and see the magic.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.