5

I have a VS template with something like

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

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

What I'd like to write is:

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

that doesn't get escaped with \c and \f and form a valid path. Is it possible?

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • You could look at raw string literals, or you could just use a forward slash and have it be a *little* more portable. – chris Jun 25 '13 at 17:49

3 Answers3

7

For paths you should be able to use a single forward slash as a separator:

std::string mypath = "c:/custom/file.jpg";
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
5

Try a raw string literal:

string mypath = R"(C:\custom\file.jpg)";
tckmn
  • 57,719
  • 27
  • 114
  • 156
1

Try to get used of double backslash character, because in c++ all parser and compiler understand that. and if your VS template \\ doublebackslash produce a \ single backslash, use 4 backslash \\\\ to produce \\ doublebackslash correctly.

arifnpm
  • 357
  • 1
  • 7
  • 1
    It just doesn't work. For I am using the one slash, it thinks that I am escaping the next character. It's okay, so I am using the double slash, a-a-and… I am gettin the double slash! Not single, but the double! Just insane… – Hi-Angel Oct 10 '14 at 11:27