In my c++ application I want to change working directory to specified one. But when I specifying "~/dev/" as working directory chdir() fails and returns -1. Instead it works when I'm specifying "/home/myusername/dev". Why this happens? How can I solve this problem (except of checking if string first charachter is ~ and replacing it with /home/myusername)?
The expansion of tilde (~) to HOME environment variable is done by bash. That expansion is done before that is passed to chdir command. The bottom line is ~ is handled by bash, not by chdir command. So you can not use it in chdir() function call.
In order to find other kind of expansions (and the details of ~ expansion) done by bash, check this link.
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