I need to move from the directory I am working on and then verify if a .txt file exists or not. The name of the .txt file is the int key value received as an argument.
What I tried to do was cast the int value into char and then append .txt. Then use this new string to try and open the file.
However, even if the file does exist within the directory, I still get "File doesnt exist".
What do I need to change in order for it to work?
    char k[10];
    char app[4] = ".txt";
    int status;
    // Change directory where we work.
    if(chdir("txtFiles") == -1){
        perror("Error while changing directories.");
        return -1;
    }
    // Convert key to string.
    snprintf(k, 10, "%d", key);
    strncat(k, app, 4);
    printf("%s\n", k);
    // Open the file.
    if((status = open(k, O_RDONLY)) == -1){
        printf("File DOESNT exist.\n");
    } else {
        printf("File exists.\n");
    }
    return 0;
}
 
     
    