2

I have file called input1.txt,which is a matrix of numbers and some letters.I am trying to read and store it in a 2D array so that each charter will be 1 cell. Here is my text file:

1111S11110
0000010001
110100010d
t001111110
0100000001
0111111101
1111111101
00000D01T1
0111110001
0000E01110

And here is my code:

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


// Function for finding the array length
int numOfLines(FILE *const mazeFile){
    int c, count;
    count = 0;
    for( ;; ){
        c = fgetc(mazeFile);
        if( c == EOF || c == '\n' )
            break;
        ++count;
    }
    return count;
}

// Main Function
int main( int argc, char **argv )
{
    // Opening the Matrix File
    FILE *mazeFile;
    mazeFile = fopen( "input1.txt", "r" );
    if( mazeFile == NULL )
        return 1;
    int matrixSize = numOfLines( mazeFile );

    // Reading text file into 2D array
    int i,j;
    char mazeArray [matrixSize][matrixSize];

    for(i=0;i<matrixSize;i++){
            for(j=0;j<matrixSize;j++){
                fscanf(mazeFile,"%c", &mazeArray[i][j]);
            }
    }

    for(i=0;i<matrixSize;i++){
        for(j=0;j<matrixSize;j++){
            printf("%c",mazeArray[i][j]);
        }
    }

    fclose( mazeFile );
    return 0;
}

However my console output is like that when i print them:

0000010001
110100010d
t001111110
0100000001
0111111101
1111111101
00000D01T1
0111110001
0000E01110@

Seems it does not read the 1st line,However in terms of indexes i think it is ok.I am new to C.Could anyone please help.Thanks in advance.

5
  • 1
    your numOfLines is a misnomer, it counts the number of characters in the first line. And therefore, reads them! Why do you think if you continue reading, you would restart from the beginning? magic? Try rewind(). Commented Oct 17, 2017 at 15:01
  • 1
    Your numOfLines() function reads the file to the end. If you want to re-read it, you need to seek back to the beginning (which is possible for regular files, but not for some other possible kinds of streams). Commented Oct 17, 2017 at 15:02
  • 1
    @JohnBollinger it doesn't read to the end, which adds to the confusion in this code... Commented Oct 17, 2017 at 15:03
  • 1
    I see you're right, @FelixPalmen. OP's misnomer tricked me, and I didn't read the code carefully enough. But the bottom line is still what we both said: if he reads from the file then he needs to seek back to the beginning before he can read the same data again. Commented Oct 17, 2017 at 15:08
  • Thanks for the idea :) Commented Oct 17, 2017 at 15:24

1 Answer 1

2

There are several problems here:

The numOfLines function is wrong. Here is the corrected version; it actually counts the number of lines and resets the file pointer to the beginning of the file.

Your version counted only the number of characters in the first line (which happens to be 10, and therefore the value seemed corrrect), and it did not reset the file pointer to the beginning of the file (therefore the first line was missing in your output).

int numOfLines(FILE *mazeFile) {  // no const here BTW !!
  int c, count;
  count = 0;
  for (;; ) {
    c = fgetc(mazeFile);
    if (c == EOF)
      break;          // enf of file => we quit

    if (c == '\n')
      ++count;        // end of line => increment line counter
  }
  rewind(mazeFile);

  return count+1;
}

Then you forgot to absorbe the \n character at the end of each line. This \n is at the end of each line of your file, but you need to read it, even if you don't want to store it in your 2d array.

  for (i = 0; i<matrixSize; i++) {
    for (j = 0; j<matrixSize; j++) {
      fscanf(mazeFile, "%c", &mazeArray[i][j]);
    }

    char eol;                         // dummy variable
    fscanf(mazeFile, "%c", &eol);     // read \n character
  }

And finally you need to print the \n for the reason mentioned above.

for (i = 0; i<matrixSize; i++) {
  for (j = 0; j<matrixSize; j++) {
    printf("%c", mazeArray[i][j]);
  }

  putc('\n', stdout);                 // print \n
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for helping me to see my mistakes :) , That is very kind of you.
@HabilGanbarli you're welcome. For clear questions you almost get always a quick and clear answer here..

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.