c++copendir

opendir error: Couldn't open directory: Mo such file or directory


I get an error with this instruction:

  dp = opendir ("%APPDATA%/.");

  output: 
  Couldn't open directory: No such file or directory.

but I don't get an error with this instruction:

dp = opendir ("C:/Users/xrobot/AppData/.");

output:
.
..
Local
LocalLow
Roaming

Why?


Solution

  • opendir doesn't expand meta variables like %APPDATA%, the shell does. So such things work from the command line, but not from a program. In your program, you have to use an absolute or relative path.

    You can probably obtain the required path with getenv(),

    const char *appData = getenv("APPDATA");
    if (appData) {
        dp = opendir(appData);
    } else {
        /* die or recover */
    }