C++ 附加到 boost::filesystem::path
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2396548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending to boost::filesystem::path
提问by Zack
I have a certain boost::filesystem::path
in hand and I'd like to append a string (or path) to it.
我boost::filesystem::path
手头有一个,我想在它上面附加一个字符串(或路径)。
boost::filesystem::path p("c:\dir");
p.append(".foo"); // should result in p pointing to c:\dir.foo
The only overload boost::filesystem::path
has of append
wants two InputIterator
s.
唯一的重载boost::filesystem::path
有 ofappend
想要两个InputIterator
s。
My solution so far is to do the following:
到目前为止,我的解决方案是执行以下操作:
boost::filesystem::path p2(std::string(p.string()).append(".foo"));
Am I missing something?
我错过了什么吗?
采纳答案by Zack
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path p (__FILE__);
std::string new_filename = p.leaf() + ".foo";
p.remove_leaf() /= new_filename;
std::cout << p << '\n';
return 0;
}
Tested with 1.37, but leafand remove_leafare also documented in 1.35. You'll need to test whether the last component of pis a filename first, if it might not be.
与1.37测试,但叶子和remove_leaf也被记录在1.35。您需要先测试p的最后一个组件是否是文件名,如果可能不是。
回答by hkaiser
If it's really just the file name extension you want to change then you are probably better off writing:
如果它真的只是您想要更改的文件扩展名,那么您最好编写:
p.replace_extension(".foo");
for most other file path operations you can use the operators /=
and /
allowing to concatenate parts of a name. For instance
对于大多数其他文件路径操作,您可以使用运算符/=
并/
允许连接名称的各个部分。例如
boost::filesystem::path p("c:\dir");
p /= "subdir";
will refer to c:\dir\subdir
.
将参考c:\dir\subdir
。
回答by martemiev
With Version 3 of Filesytem library (Boost 1.55.0) it's as easy as just
使用 Filesytem 库 (Boost 1.55.0) 的第 3 版,就这么简单
boost::filesystem::path p("one_path");
p += "_and_another_one";
resulting in p = "one_path_and_another_one"
.
导致p = "one_path_and_another_one"
.
回答by leon le bourdon
path p;
std::string st = "yoo";
p /= st + ".foo";
回答by phinz
You can define the +
operator yourself such that you can add two boost::filesystem::path
variables.
您可以+
自己定义运算符,以便可以添加两个boost::filesystem::path
变量。
inline boost::filesystem::path operator+(boost::filesystem::path left, boost::filesystem::path right){return boost::filesystem::path(left)+=right;}
Then you can even add a std::string
variable (implicit conversion). This is similar to the definition of the operator/
from
然后你甚至可以添加一个std::string
变量(隐式转换)。这类似于operator/
from的定义
include/boost/filesystem/path.hpp:
包括/boost/filesystem/path.hpp:
inline path operator/(const path& lhs, const path& rhs) { return path(lhs) /= rhs; }
Here is a working example:
这是一个工作示例:
main.cpp:
主.cpp:
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
inline path operator+(path left, path right){return path(left)+=right;}
int main() {
path p1 = "/base/path";
path p2 = "/add/this";
std::string extension=".ext";
std::cout << p1+p2+extension << '\n';
return 0;
}
compiled with
编译与
g++ main.cpp -lboost_system -lboost_filesystem
produces the output:
产生输出:
$ ./a.out
"/base/path/add/this.ext"