Possible Duplicate:
How can I create directory tree in C++/Linux?
Why mkdir fails to work with tilde (~)?
i'm trying to create a directory in a C program and i use the mkdir function. My program is as follows:
#include <stdio.h>
#include <string.h>
#define MKDIR(x) mkdir(x)
int main() {
//If i do mkdir("foo"), the dir is created
mkdir("~/test/foo"); //Directory foo not created inside test dir
}
The dir foo isn't created in Test dir.
But how can i achieve that? Thanks, in advance
mkdir()
function doesn't expand ~
shortcut, you'll have to pull the value from the HOME
environment variable. (see man getenv
).
check wordexp: http://pubs.opengroup.org/onlinepubs/7908799/xsh/wordexp.html
#include <wordexp.h>
#include <stdio.h>
int main() {
wordexp_t p;
if (wordexp("~/", &p, 0)==0) {
printf("%s\n", p.we_wordv[0]);
wordfree(&p);
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With