我用C ++来处理很多文件,我必须在源代码中写这样的文件名: "F:\\\\somepath\\\\subpath\\\\myfile"
,我想知道如果有什么方法可以摆脱键入“\\ \\“在字符串文字上下文中获取一个字符'\\',即我希望我只能写出"F:\\somepath\\subpath\\myfile"
而不是无聊的字符。
Solutions:
use C++11 string literals: R"(F:\\somepath\\subpath\\myfile)"
Use boost::path with forward slashes: They will validate your path and raise exceptions for problems.
boost::filesystem::path p = "f:/somepath/subpath"; p /= "myfile";
just use forward slashes; Windows should understand them.
If you have C++11, you can use raw string literals:
std::string s = R"F:\somepath\subpath\myfile";
On the other hand, you can just use forward slashes for filesystem paths:
std::string s = "F:/somepath/subpath/myfile";
Two obvious options:
C++11 has raw string literals. Stuff inside them doesn't need to be escaped.
R"(F:\\somepath\\subpath\\myfile)"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.