C++ 使用boost创建相对路径

C++ 使用boost创建相对路径,c++,boost,filesystems,relative-path,C++,Boost,Filesystems,Relative Path,我正在尝试使用boost创建相对路径 我最初的计划是: string base_directory; // input boost::filesystem::path base_path; string other_directory; // input boost::filesystem::path other_path; // assume base_path is absolute - did that already (using complete() // if path is

我正在尝试使用boost创建相对路径

我最初的计划是:

string base_directory;  // input
boost::filesystem::path base_path;
string other_directory;  // input
boost::filesystem::path other_path;
// assume base_path is absolute - did that already (using complete() 
// if path is relative, to root it in the current directory)  -> 
base_directory = base_path.string();

if (other_path.empty())
  other_directory = base_directory;
else
{
  other_path = boost::filesystem::path(other_directory);        
  if(!other_path.is_complete())
  {
    other_path = base_path / other_path;
    other_directory = other_path.string();          
  }
  if(!boost::filesystem::exists(boost::filesystem::path(other_path)))
  {
    boost::filesystem::create_directory(other_path);
  }
}
如果其他_目录是绝对的或只是一个名称(或与基本_目录相对的内部目录),则这种方法可以很好地工作

但如果我尝试放置“.”或“./other”,我会得到一些奇怪的构造,比如“c:\test..”或“c:\test..\other”

如何正确创建相对路径(最好使用boost)?我试图查看文档。。。没有积极的成功

我正在使用Windows(我对boost的偏好是它应该是多平台的,我已经依赖它了)

编辑:我有boost 1.47


谢谢您的建议。

Boost文件系统不知道
“C:\test”
是指文件还是指目录,因此它不会假定尾部的
“\”
是正确的

如果添加该
“\”
,则可以使用该函数简化删除
元素的路径

other_path = boost::filesystem::path( other_directory + "\" );        
if(!other_path.is_complete())
{
  other_path = boost::filesystem::canonical( base_path / other_path );

谢谢,但好像没有。我有boost 1.47(我不认为它有什么不同),根据你的建议,我在文档中查找并发现了一个“规范化”,但它没有显示在boost::filesystem(或filesystem3)列表中如果没有“规范化”它的选项,我将接受这个选项,我不知道这确实是一条有效的道路——只有在回答了这个问题之后,我才有勇气去检验它。@Thalia你离得太近了!我做了一些研究,并添加了该函数:(