Vlad Keel
Vlad Keel

Reputation: 400

Printing boost path on windows with escaped backslashes

I need to print out a path (stored as boost filesystem path) to file, to be parsed back to path later. The parser expects paths in windows platform to be escaped, so a path like

c:\path\to\file

will appear in the file as

c:\\path\\to\\file

Is there a method in boost path to do this? or do i need to process the output of string() method to add the escapes?

Upvotes: 1

Views: 842

Answers (1)

sehe
sehe

Reputation: 394054

Did you hear about std::quoted?

It can be handy for things like this. Alternatively, use the power of your shell (e.g. Escape FileNames Using The Same Way Bash Do It)

Live On Coliru

#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::quoted(R"(c:\path\to\file)") << std::endl;
    std::cout << std::quoted("c:\\path\\to\\file") << std::endl;
}

Prints

"c:\\path\\to\\file"
"c:\\path\\to\\file"

Note: also shows raw string literal

Upvotes: 3

Related Questions