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

I have a VS template with something like

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

I would like to make C: \ custom \ part with the template substitution parameter $ userpath $. Is there a way to avoid double slashes?

I would like to write:

 string mypath = SOMETHING("C:\custom\file.jpg") 

which does not receive the escape code with \ c and \ f and forms a valid path. Is it possible?

+4
source share
3 answers

For paths, you should use a single slash as a separator:

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

Try a string literal:

 string mypath = R"(C:\custom\file.jpg)"; 
+3
source

Try using a double backslash character, because in C ++ all the parser and compiler understand this. and if your VS \\ doublebackslash pattern creates a single backslash \ , use 4 backslashes \\\\ to display \\ doublebackslash correctly.

+1
source

Source: https://habr.com/ru/post/1488137/


All Articles