Reputation: 26565
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
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
Reputation: 60037
Because the first opendir
is LITERALLY trying to open the directory %APPDATA%/.
.
Upvotes: 2