Use shell characters in directory path

I want to open a file with my program and I want to use shell characters to make it easier for me to find the file. Is there an easy way to have the shell expand my filepath before using it at runtime. I am looking for a function that does this.

~/.foo.bar

/home/someuser/.foo.bar

Is there any easy way to get the shell to preprocess file paths before opening the file?

+3


source to share


1 answer


You can use wordexp :



#include <wordexp.h>

std::string wordexp(std::string var, int flags = 0)
{
    wordexp_t p;
    if(!wordexp(var.c_str(), &p, flags))
    {
        if(p.we_wordc && p.we_wordv[0])
            var = p.we_wordv[0];
        wordfree(&p);
    }
    return var;
}

int main()
{
    std::cout << wordexp("~/test") << '\n';
}

      

+2


source







All Articles