0

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();
}
  • 1
    Please post a sample input file. – dbush Mar 11 '20 at 18:30
  • You don't show all the code. Does it call `scanf` before calling `fgets`? If so please be aware that [`fgets` doesn't work after `scanf`](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf) because of the newline left in the input buffer. – Weather Vane Mar 11 '20 at 18:41
  • 2
    "l but nothing is ever printed like the program skipped to the end", in the code you show there is no code to print anytthing, a [mimimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) would increase the chances of an acceptable answer. A newline character in the input buffer is likely the cause, but it's impossible to be sure with the code you present. – anastaciu Mar 11 '20 at 18:43
  • 1
    Assuming your input file is well formed, this code should work. Either there is a problem with the file or there's a problem somewhere else in your code. Please update your question with a [mcve] that others can compile and run *as is*. Also, try running your code through valgrind. If you're mismanaging memory, it will tell you where. – dbush Mar 11 '20 at 19:05
  • 1
    Hmm, you do not test the result of `fopen` with a relative path and next input operation on the file breaks. Chances are that the current path is not what you imagine and that `file` is `NULL` after open... – Serge Ballesta Mar 11 '20 at 21:51
  • the array: `maze[][]` is not defined anywhere in the posted code. Suggest using the `variable length array` feature of C and define `maze` just before reading the second line of the input file – user3629249 Mar 12 '20 at 00:12
  • when calling: `fgets()`, always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Mar 12 '20 at 00:14
  • when calling: `strtok()`, always check (!=NULL) the returned value to assure the operation was successful – user3629249 Mar 12 '20 at 00:15
  • every time this: `maze[r][c] = strtok(NULL, " ")[0];` is executed, another 'column' is pointed toward. Is each column only a single character? Are all columns separated via a space? Suggest re-thinking this part of the code – user3629249 Mar 12 '20 at 00:18
  • Your complaint is nothing is printed, however, there is/are no `printf()` statements in the posted code. – user3629249 Mar 12 '20 at 00:20
  • I literally posted the PROBLEM function everything outside of this function has been testing and works with perfection. It was code I wrote on unix and used pipes and stdin to use the file and I am trying to use fopen instead. The maze is plenty large enough, of course I print somewhere else this function is called LOADmaze so I don't know why it would be printing here. I will post the full code but this is the only thing that could have a problem and that's why I only posted it – Coltyn Stone-Lamontagne Mar 12 '20 at 14:14
  • The file IS loading a NULL and I have no idea why it is not working... – Coltyn Stone-Lamontagne Mar 12 '20 at 14:45

0 Answers0