Lara
Lara

Reputation: 21

Trying to print an array out of a file

I'm new here and new to C.

I want to print an array out of a file with fopen but it does not seem to work.

My empty array is char matrix[25][25], and now I am trying to open a text file and print it into the array. The txt file is named Matrix1.txt and consists out of 625 characters that are either '*' or ' '.

What I am trying to do now is:

//openfile(), LÄNGE=Lenght=25, BREITE=Height=25, datei=file, matrix=array
int dateiöffnen(char matrix[][LÄNGE], char* datei){

    FILE *fp;
    char cell;

    fp = fopen(datei, "r");
    if (fp == NULL){
        printf("Fehler!\n");  //Fehler=Error
    }
    else{
        for (int y = 0; y < BREITE; y++){
            for (int x = 0; x > LÄNGE; x++){
                fscanf(fp, "%c", &cell);
                matrix[x][y] = cell;
            }
        }

        fclose(fp);
    } 
}

So later I try to print the array and play the Game of life with that printed array (it's in a switch):

case 1: 
    dateiöffnen(matrix, "Matrix1.txt");
    play(matrix); 
    print(matrix);

but for some reason it prints a blank array and if I try to run the openfile() in the main function like this

int main(int argc, char *argv[]) {
    char matrix[BREITE][LÄNGE];
    int x, y;
    //srand(time(NULL));
    dateiöffnen(matrix, "Matrix1.txt");
}

it prints an array like this:

this

So I am very confused and would be glad and thankful if someone could give me a hint what's going on.

Upvotes: 2

Views: 69

Answers (1)

clemens
clemens

Reputation: 17721

There is probably a typo in the inner for loop:

for (int x = 0; x > LÄNGE; x++){

should be:

for (int x = 0; x < LÄNGE; x++){

Upvotes: 1

Related Questions