简体   繁体   English

在Linux中使用RealPath将相对路径更改为C ++中的绝对路径

[英]Change relative path to absolute path in C++ using realpath in Linux

I need to change a relative path to absolute path in linux in C++. 我需要在C ++中的linux中将相对路径更改为绝对路径。 I am using realpath, but the output is wrong! 我正在使用realpath,但是输出错误!

char resolved_path[200];
realpath("$HOME/Desktop/SumoSVN/bin", resolved_path);
cout << resolved_path << endl;

I am expecting to get this output : /home/mani/Desktop/SumoSVN/bin 我期望得到以下输出:/ home / mani / Desktop / SumoSVN / bin

but I am getting this!: /home/mani/Desktop/VENTOS/$HOME 但我得到这个!:/ home / mani / Desktop / VENTOS / $ HOME

what am I doing wrong? 我究竟做错了什么?

Shell or environment variables (see environ(7) ) are not expanded by realpath(3) . Shell或环境变量(请参阅environ(7) )不会由realpath(3)扩展。 You need to call getenv(3) . 您需要调用getenv(3) You could try 你可以试试

std::string homedir(getenv("HOME"));
realpath((homedir+"/Desktop/SumoSVN/bin").c_str(), resolved_path);

See also wordexp(3) (and perhaps glob(3) ). 另请参见wordexp(3) (可能还有glob(3) )。 Read path_resolution(7) & glob(7) . 读取path_resolution(7)glob(7) Notice that it is your shell which expands arguments of commands. 注意,这是您的shell扩展命令的参数。

BTW, the current directory is obtained by getcwd(3) . 顺便说一句,当前目录是通过getcwd(3)获得的。

PS. PS。 On some installations with different languages Desktop does not exist: it becomes eg Bureau on French Debian or Ubuntu or Mint systems. 在某些使用不同语言的安装中,不存在Desktop :例如,它成为法语Debian或Ubuntu或Mint系统上的Bureau

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM