Use shell symbols in directory path

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 16:46:14

问题


I want to open a file with my program, and I want to use shell symbols to make it easier for me to locate the file. Is there an easy way to have the shell expand out my file path before I use it on runtime. I'm looking for a function that does this.

~/.foo.bar -> /home/someuser/.foo.bar

Is there some easy way to have the shell preprocess the paths to files before opening the file?


回答1:


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';
}


来源:https://stackoverflow.com/questions/31035974/use-shell-symbols-in-directory-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!