有时候需要把相对路径转换成绝对路径,然后判断路径里是否包含“目标路径”,然后进行下一步操作,比如说某个配置文件需要特殊处理,而且这个配置文件的路径是不变的。
#include <iostream>
std::string absolutePath(std::string path)
{
#ifdef _WIN32
char absPath[4096] = {0};
_fullpath(absPath, path.c_str(), 4096);
#else
//linux 需要大点的空间
char absPath[40960] = {0};
realpath(path.c_str(), absPath);
#endif
return std::string(absPath);
}
int main()
{
// 在exe所在目录添加个log4cplus.config文件,验证一下.
std::cout<<absolutePath("log4cplus.config")<<std::endl;
return 0;
}
原文链接:https://blog.youkuaiyun.com/caoshangpa/article/details/52804973