如文档中所述,以下预期输出为:
boost::filesystem::path filePath1 = "/home/user/";
cout << filePath1.parent_path() << endl; // outputs "/home/user"
boost::filesystem::path filePath2 = "/home/user";
cout << filePath2.parent_path() << endl; // outputs "/home"
Run Code Online (Sandbox Code Playgroud)
问题是,你如何处理这个问题?也就是说,如果我接受一个路径作为参数,我不希望用户关心它是否应该有一个尾部斜杠.看起来最简单的事情就是附加一个尾部斜杠,然后调用parent_path()TWICE来获取我想要的"/ home"的父路径:
boost::filesystem::path filePath1 = "/home/user/";
filePath1 /= "/";
cout << filePath1.parent_path().parent_path() << endl; // outputs "/home"
boost::filesystem::path filePath2 = "/home/user";
filePath2 /= "/";
cout << filePath2.parent_path().parent_path() << endl; // outputs "/home"
Run Code Online (Sandbox Code Playgroud)
但这看起来很荒谬.在框架内有更好的方法来处理这个问题吗?