0

I try to read a grid from a file into a 2 dimensional array. The program compiles without any errors. Here is the code:

    #include <stdio.h>
    #include <stdlib.h>

    FILE* openFile(FILE* file, char* name, char* mode) {
        file = NULL;
        file = fopen(name, mode);
        if(file == NULL) {
            printf("Could not open a file!\n");
            exit(1);
        } else {
            printf("File was opened/created successfully!\n\n");
        }

        return file;
    }

    int main() {
        FILE* file;
        file = openFile(file, "a.txt", "r");

        char c;
        int x, row, column;
        x = row = column = 0;

        int array[2][2];
        for(int i = 0; i < 2; i++) {
            for(int j = 0; j < 2; j++) {
                array[i][j] = 0;
            }
        }

        while(!feof(file) && (c = fgetc(file))) {
            if(c == '\n') {
                row++;
            }

            if(c != '\n' && c!= '\r') {
                x = atoi(&c);
                if(array[row][column] == 0) {
                    array[row][column] = x;
                    printf("array[%d][%d] = %d\n", row, column, array[row][column]);
                    printf("row = %d\n", row);
                    printf("column = %d\n\n", column);
                    column++;
                }
            }
        }

        for(int i = 0; i < row; i++) {
            for(int j = 0; j < column; j++) {
                printf("array[%d][%d] = %d\n", i, j, array[i][j]);
            }
            printf("\n");
        }

        fclose(file);
        return 0;
    }

txt file:

    02
    46

Output of the program:

    File was opened/created successfully!

    array[0][0] = 0
    row = 0
    column = 0

    array[0][1] = 2
    row = 0
    column = 1

    array[0][0] = 0
    array[0][1] = 2

Seems like it reads only the first line and then feof() returns that it has reached the end. I have visited a few websites trying to understand what is wrong.

Could anyone explain where is the mistake i made and show the correct solution?

  • 1
    http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Bob__ Jul 26 '16 at 09:03
  • 2
    reset the `column` value to `0` when moving to another row – dvhh Jul 26 '16 at 09:04
  • 2
    OT (dvhh solved the issue), why are you passing file to openFile? Just return it. – Bob__ Jul 26 '16 at 09:10
  • 1
    `while(!feof(file) && (c = fgetc(file))) {` --> `while((c = fgetc(file)) && !feof(file)) {`, `x = atoi(&c);` --> `x = c - '0';` – BLUEPIXY Jul 26 '16 at 09:27
  • Never use `feof()` as a loop control. It does not work as expected. A much better method is to use the returned value from `fgetc()` Suggest: `while( (c = fgetc(file)) && EOF != c) {` – user3629249 Jul 27 '16 at 14:10

1 Answers1

0

You are not reseting the column number when changing row. The 2nd line in your file is save in array[1][2] and array[1][3] but you don't display it.

while(!feof(file) && (c = fgetc(file))) {
    if(c == '\n') {
        row++;
        column = 0;
    }

This will normally work.

M.Ferru
  • 400
  • 1
  • 6
  • 20
  • In your code, you were not writing in a allocated area. This can cause massive issue in embedded system ! Think about it next time :) – M.Ferru Jul 26 '16 at 09:55
  • 1
    Please consider advising the OP about [issues in checking condition](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) `while(!feof(file))`, or writing code which does not repeat it. – user3078414 Jul 26 '16 at 14:19
  • Thanks for useful information about `feof()` function. – Kirill Saidov Jul 27 '16 at 08:07