双斜杠//在路径中-我可以使用单斜杠吗

Double slash // in paths - can I use single slash?

本文关键字:可以使 我可以 单斜杠 路径      更新时间:2023-10-16

我有一个类似的VS模板

string mypath = "C:\custom\file.jpg";

我想用模板替换参数$userpath$制作C:\custom\部件。有什么办法可以避免使用双斜线吗?

我想写的是:

string mypath = SOMETHING("C:customfile.jpg")

不会用\c和\f进行转义并形成有效路径。有可能吗?

对于路径,您应该能够使用单个正向斜线作为分隔符:

std::string mypath = "c:/custom/file.jpg";

尝试一个原始字符串文字:

string mypath = R"(C:customfile.jpg)";

尽量使用双反斜杠字符,因为在c++中,所有解析器和编译器都能理解这一点。如果VS模板\双反斜杠生成单反斜杠,请使用4反斜杠\\正确生成\双反斜杠。