提高::文件系统获得相对路径 [英] boost::filesystem get relative path
本文介绍了提高::文件系统获得相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么的boost ::文件系统的方法
库可以帮助我获得相对于另一路径的路径?
What methods of the boost::filesystem
library can help me to get a path relative to another path?
我有一个路径 /家庭/用户1 /下载/图书
和路径 /家庭/用户1 /
。现在,我希望得到一个路径下载/图书
。
I have a path /home/user1/Downloads/Books
and a path /home/user1/
. Now I want to get a path Downloads/Books
.
推荐答案
从以下尼科尔挂票找到了一个链接拍摄
Taken from a link found by following the ticket Nicol linked to:
template < >
path& path::append< typename path::iterator >( typename path::iterator begin, typename path::iterator end, const codecvt_type& cvt)
{
for( ; begin != end ; ++begin )
*this /= *begin;
return *this;
}
// Return path when appended to a_From will resolve to same as a_To
boost::filesystem::path make_relative( boost::filesystem::path a_From, boost::filesystem::path a_To )
{
a_From = boost::filesystem::absolute( a_From ); a_To = boost::filesystem::absolute( a_To );
boost::filesystem::path ret;
boost::filesystem::path::const_iterator itrFrom( a_From.begin() ), itrTo( a_To.begin() );
// Find common base
for( boost::filesystem::path::const_iterator toEnd( a_To.end() ), fromEnd( a_From.end() ) ; itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo; ++itrFrom, ++itrTo );
// Navigate backwards in directory to reach previously found base
for( boost::filesystem::path::const_iterator fromEnd( a_From.end() ); itrFrom != fromEnd; ++itrFrom )
{
if( (*itrFrom) != "." )
ret /= "..";
}
// Now navigate down the directory branch
ret.append( itrTo, a_To.end() );
return ret;
}
坚持在一个头文件,它应该做你想做的。
Stick that in a header file and it should do what you want.
样的呼叫:
boost::filesystem::path a("foo/bar"), b("foo/test/korv.txt");
std::cout << make_relative( a, b ).string() << std::endl;
这篇关于提高::文件系统获得相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文