How to pass a string literal ("~ / test / foo") to the mkdir function in C programming?

Possible duplicate:
How to create a directory tree in C ++ / Linux? Why doesn't mkdir work with tilde (~)?

I am trying to create a directory in a C program and I am using 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 } 

dir foo is not created in the test directory.

But how can I achieve this? Thanks in advance

+4
source share
2 answers

mkdir() function does not extend the shortcut ~ , you will have to pull the value from the HOME environment variable. (see man getenv ).

+9
source

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; } 
+1
source

Source: https://habr.com/ru/post/1383226/


All Articles