1

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?

Vlad Keel
  • 372
  • 2
  • 13
  • The _"escaping"_ of `\ ` is only needed in character array or string literals. You must be confusing something. –  Jan 28 '18 at 13:17
  • @TheDude I think it's pretty clearly stated that whatever this other parser program is, it actually wants doubled backslashes in the text file, for whatever reason. – aschepler Jan 28 '18 at 13:19
  • 1
    @aschepler In that case I don't see a problem with simply using [`std::string::replace()`](http://en.cppreference.com/w/cpp/string/basic_string/replace) before saving to the file. –  Jan 28 '18 at 14:17

1 Answers1

3

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • @VladKeel it's not hard to make something similar: http://coliru.stacked-crooked.com/a/14c764b4661e2318 – sehe Feb 13 '18 at 09:18
  • Oh, that was obviously meant to be compiled [in c++03 mode](http://coliru.stacked-crooked.com/a/5b618e02d2c6fa06) – sehe Feb 13 '18 at 10:11