I cannot get fgets to work with fopen in C
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)
The function in question is:
//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();
}
Please sign in to leave a comment.