0

I am stuck in loading letters from text file into 2D array.

my code is like below;;

#include <stdio.h>

int main() {
    int i, j, a, b;
    char c;
    char f, filename[100];
    char match[i][j];
    FILE *fp;

    fp = fopen(filename, "r");
    for (a = 0; a < i; a++) {
        for (b = 0; b < j; b++) {
            match[i][j] = '.';
        }
    }

    while ((c = fgetc(fp)) != EOF) {
        for (a = 0; a < i; a++) {
            for (b = 0; b < j; b++)
                if ((c != '\n') && (c != '\r'))
                    match[a][b] = c;
        }
    }

    for (a = 0; a < i; a++) {
        for (b = 0; b < j; b++)
            printf("%c", match[a][b]);
        printf("\n");
    }

    return 0;
}

and text file contains letters like:
AAAAA\nBBBBB\nCCCCC\nDDDDD\nEEEEF\n

I want to load text file to program exactly same, but it is only showing the result like: FFFFF\nFFFFF\nFFFFF\nFFFFF\nFFFFF\n

can you please tell me where is the problem?

2
  • 2
    You read i and j uninitialized to determine the size of match. Commented Aug 17, 2014 at 17:39
  • I would expect the code you are showing would give compilation errors. i, j, and filename are all uninitialized. Commented Aug 17, 2014 at 18:04

2 Answers 2

1

As stated by Ahmed, you never inititalize i, j, and filename in shown code.

Assuming you do it (with your input, I assume i=5, j=5), you first initialize all match array to . : ok.

Then you read your file character by character and control is it is different from '\n' and \r. Let's look what happens at last character of last word, which is a F. You loop on full array (a in [0;i[, b in [0; j[), and if character if neither \r, nor \n (it is not) you affect it to match[i, j]

for (a = 0; a < i; a++) {
    for (b = 0; b < j; b++)
        if ((c != '\n') && (c != '\r'))
            match[a][b] = c;
}

So at the end of the loop, match only contains F, what explains current output ...

You have still another problem, you affect char c = fgetc(fp), and compare it to EOF. That's dangerous, because fgetc() and EOF are both declared as int.

The fgetc loop should be in innermost position to work correctly, only skipping '\n' and \r :

int c; 
...
for (a = 0; a < i; a++) {
    for (b = 0; b < j; b++)
        while(((c = fgetc(fp)) != EOF) && ((c == '\n') || (c == '\r'))) /* skip eol /*
            if (c != EOF) 
                match[a][b] = c;
}

This should be enough to fix current errors, but anyway the general program organization is bad. Please write as pseudo code what should happen before coding if you are a beginner. Until the algorithm and structure is good, code will be bad ...

Sign up to request clarification or add additional context in comments.

Comments

1
  1. char match[i][j] this gives compilation error define array with constant
  2. scanf("%d%d%s", &i,&j,&filename); here instead of &filename write filename because array name is address of array there is no need of &
  3. when typing file name make sure you type complete address like this "c:\\documents\\filename.txt" note here \ or use can write it as "c:/documents/filename.txt"

Comments

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.