here is my code


    FILE * paths = fopen(CONFIGS_TXT, "r");
    FILE * temp = fopen(TEMP_FILE, "w");

    fgets(buf, 100, paths);
    token = strtok(buf, "=");
    token = strtok(NULL, "\n");
    if(token == NULL) {
        token = getPathFromUser();
    }
    fprintf(temp, "DOLPHIN_PATH=%s\n", token);

    fclose(paths);
    fclose(temp);
    
    errno = 0;
    remove(CONFIGS_TXT);
    perror("The following error occurred");

    errno = 0;
    rename(TEMP_FILE, CONFIGS_TXT);
    perror("The following error occurred");

when i run it i get


The following error occurred: No such file or directory
The following error occurred: No such file or directory

how is it even possible for me to get that error when reading and writing to the 2 files was successful?

it is fixed by commenting out my getPathFromUser() function which looks like this


char * getPathFromUser() {
    
    OPENFILENAME ofn;
    char fileName[MAX_PATH] = "";
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = "executables (*.exe*)\0*.exe*\0";
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "";

    char * filePath;

    if(GetOpenFileName(&ofn))
        filePath = fileName;

    return filePath;
}

1

There are 1 best solutions below

0
On
char fileName[MAX_PATH] = "";
...
char * filePath;
...
filePath = fileName;
...
return filePath;

This is undefined behaviour as you are returning a pointer to a local variable.