如何从另一个减去一个路径? [英] how to subtract one path from another?

查看:216
本文介绍了如何从另一个减去一个路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...我有一个基本路径和新的path.New路径包含在它的基本路径。我需要看到的是在新的路径不同。像我们有/家庭/和新的路径是/ home /苹果/之一,我需要从它的苹果/一个拿到。注意 - 当我将创建(HOMEPATH / diffPath)一些路径,我需要得到/再回家/苹果/之一。如何做这样的事与提高文件系统?

So... I have a base path and a new path.New path contains in it base path. I need to see what is different in new path. Like we had /home/ and new path is /home/apple/one and I need to get from it apple/one. note - when I would create some path from (homePath/diffPath) I need to get that /home/apple/one again. How to do such thing with Boost FileSystem?

推荐答案

利用干()和PARENT_PATH()和新路径向后走,直到我们回到基本路径,这工作,但我不知道这是否是非常安全的。
要谨慎,因为路径/家和/家庭/被视为不同的路径。如果基本路径是/ home(没有后面的斜杠)和新路径保证低于基本路径在目录树的下面才会工作。

Using stem() and parent_path() and walk backwards from the new path until we get back to base path, this works, but I am not sure if it is very safe. Be cautious, as the path "/home" and "/home/" are treated as different paths. The below only works if base path is /home (without trailing slash) and new path is guaranteed to be below base path in the directory tree.

#include <iostream>
#include <boost/filesystem.hpp>
int main(void)
{
  namespace fs = boost::filesystem;

  fs::path basepath("/home");
  fs::path newpath("/home/apple/one");
  fs::path diffpath;

  fs::path tmppath = newpath;
  while(tmppath != basepath) {
    diffpath = tmppath.stem() / diffpath;
    tmppath = tmppath.parent_path();
  }

  std::cout << "basepath: " << basepath << std::endl;
  std::cout << "newpath: " << newpath << std::endl;
  std::cout << "diffpath: " << diffpath << std::endl;
  std::cout << "basepath/diffpath: " << basepath/diffpath << std::endl;

  return 0;
}

这篇关于如何从另一个减去一个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆