问题
I use C++ to process many files, and I have to write the file name in source code like this:
"F:\\somepath\\subpath\\myfile"
,
I wonder that if there's any way to get rid of typing "\\" to get a character '\' in string literal context, i.e, I hope I can just write "F:\somepath\subpath\myfile"
instead the boring one.
回答1:
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.
回答2:
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";
回答3:
Two obvious options:
- Windows understands forward slashes (or rather, it translates them to backslashes); use those instead.
C++11 has raw string literals. Stuff inside them doesn't need to be escaped.
R"(F:\somepath\subpath\myfile)"
来源:https://stackoverflow.com/questions/17164769/how-to-disable-the-escape-sequence-in-c