简体   繁体   中英

Is there a way to output the backslash symbol in cout from a string?

C++ code

fs::path pathToShow("C:\Windows\system.ini");
cout << "exists() = " << fs::exists(pathToShow) << "\n"
    << "root_name() = " << pathToShow.root_name() << "\n"
    << "root_path() = " << pathToShow.root_path() << "\n"
    << "relative_path() = " << pathToShow.relative_path() << "\n"
    << "parent_path() = " << pathToShow.parent_path() << "\n"
    << "filename() = " << pathToShow.filename() << "\n"
    << "stem() = " << pathToShow.stem() << "\n"
    << "extension() = " << pathToShow.extension() << "\n";

Taken from an example from https://www.codingame.com

Output

exists() = 0
root_name() = "C:"
root_path() = "C:"
relative_path() = "Windowssystem.ini"
parent_path() = "C:"
filename() = "Windowssystem.ini"
stem() = "Windowssystem"
extension() = ".ini"
Press any key to continue . . .

Does Visual Studio have an compiler/linker option to threat the '\' symbol as

\\

when outputing with cout or printf? Or is there another simple way without making an own function to manipulate the string (without too much hazzle)?

Not a compiler option, but a language feature: use a raw string literal . Should work on all compilers.

fs::path pathToShow(R"(C:\Windows\system.ini)");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM