xRobot
xRobot

Reputation: 26565

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?

Upvotes: 0

Views: 1191

Answers (2)

Daniel Fischer
Daniel Fischer

Reputation: 183978

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 */
}

Upvotes: 7

Ed Heal
Ed Heal

Reputation: 60037

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

Upvotes: 2

Related Questions