如何在C++中打印\"

use*_*171 4 c++ printing ofstream quotations

我需要打印一个完全正确的字符串:

std::string("-I\"/path/to/dir\" ");
Run Code Online (Sandbox Code Playgroud)

基本上,我需要这样做,因为我使用C++代码生成C++代码.

我想通过ofstream编写上面的字符串,所以像

 ofstream fout;
 fout << the_string << endl;
Run Code Online (Sandbox Code Playgroud)

问题是我不能\\"在字符串里面做.

seh*_*ehe 5

只是逃避斜线报价!即\"- >\\\"

fout << "std::string(\"-I\\\"/path/to/dir\\\" \");" << std::endl;
Run Code Online (Sandbox Code Playgroud)

在C++ 0x/C++ 11中

fout << R"(std::string("-I\"/path/to/dir\" ");)" << std::endl;
Run Code Online (Sandbox Code Playgroud)

它使用原始字符串文字 1

请参阅http://ideone.com/TgtZK上的两个版本  

1毫无疑问,ideone.com和stackoverflow的语法高亮显示器尚未准备好:)