I am trying to fopen a file, read information from it and then fclose the file. In the same directory as the code is the .txt I would like to read. When I compile there are no warnings (and obviously no errors). When I go line by line in the debugger it exits with the code in the title but when I run it like normal it exits with 0 like normal but nothing is ever printed like the program skipped to the end. The printf statements are in other functions and this is the only function that accesses a file so there are no other fgets, scanf, etc.
I am using CLion if that helps. And all of the code outside of this function is fully tested, It just used to use stdin and I would like to use fopen for this is the only function that needs changing.
Sample input in a maze3.txt file:
6 5
1 1 1 1 1
1 0 0 e 1
1 1 1 0 1
1 r 1 0 1
1 0 0 0 1
1 1 1 1 1
NEW INFORMATION: Why is the file loading as NULL? I do not understand what is wrong... Also this function is just supposed to store it in the global 2D array as is so you have all you need to try it yourself now
For reference: these are global and fully initialized.
maze: 2D char array
MAX_DIMENSION: final int variable = 20;
I marked the fgets and open with ***********************
The function in question is loadMaze(); here is a minimal version for testing:
#define MAX_DIMENSION 20
char maze[MAX_DIMENSION][MAX_DIMENSION];
int mazeRows;
int mazeCols;
/*This is here to allow you to print the maze to see if it loaded correctly(it
should not have spaces or the 2 numbers from the first row of the input but
should match otherwise)
*/
void printMaze(){
checkState();
printf("\n");//space for ease of reading
for (int r = 0; r < mazeRows; ++r) {
for (int c = 0; c < mazeCols; ++c) {
printf("%c ", maze[r][c]);
}
printf("\n");
}
}
//this function loads the maze by using the standard input of a text file with specific formatting
void loadMaze(){
//file loading stuff
//********************************************************
FILE * file = fopen("maze3.txt", "r");
int lineLength = MAX_DIMENSION*2 + 2;//this will be able to store all 20 with a space after and between
char string[lineLength];//this is the longest line that should be allowed as input with a bit of room for an accidental space
//gets the rows AND cols count
//********************************************************
fgets(string, lineLength, file);//THIS IS WHERE IS STOPS IF I AM GOING LINE BY LINE IN THE DEBUGGER
mazeRows = (int)strtol(strtok(string, " "), NULL, 10);//takes the first token and extracts the integer value to set as the rows
mazeCols = (int)strtol(strtok(NULL, " "), NULL, 10);//takes the second token and extracts the integer value to set as the cols
//copies the maze into the arrays
for (int r = 0; r < mazeRows; ++r) {
//********************************************************
fgets(string, lineLength, file);
maze[r][0] = strtok(string, " ")[0];//puts the first char in the maze
for (int c = 1; c < mazeCols; ++c) {
maze[r][c] = strtok(NULL, " ")[0];//puts the next char in the maze
}
}
fclose(file);
checkState();
}
int main(){
loadMaze();
printMaze();
}