0

I get an error with this istruction:

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

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

but I don't get an erro with this istruction:

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

output:
.
..
Local
LocalLow
Roaming

Why ?

xRobot
  • 25,579
  • 69
  • 184
  • 304

2 Answers2

7

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 */
}
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • I'm no Windows expert, but you'd probably have to use `getenv()` for that. Try `const char* appData = getenv("APPDATA"); dp = opendir(appData);` – Component 10 Apr 20 '12 at 12:40
  • I don't know about C++, in C you could use `getenv` to look up the value of `APPDATA`. Well, at least on *nixish systems, not sure about Windows. – Daniel Fischer Apr 20 '12 at 12:40
2

Because the first opendir is LITERALLY trying to open the directory %APPDATA%/..

Ed Heal
  • 59,252
  • 17
  • 87
  • 127