我需要打印一个字符串来准确说明:
std::string("-I\"/path/to/dir\" ");
基本上,我需要这样做,因为我正在使用 C++ 代码生成 C++ 代码。
我想通过 ofstream 写上面的字符串,所以像
ofstream fout;
fout << the_string << endl;
问题是我不能在字符串中执行 \\"
。
最佳答案
只需转义斜杠以及引号! IE。 \"
--> \\\"
fout << "std::string(\"-I\\\"/path/to/dir\\\" \");" << std::endl;
在 C++0x/C++11 中
fout << R"(std::string("-I\"/path/to/dir\" ");)" << std::endl;
它使用了一个 raw string literal 1
在 http://ideone.com/TgtZK 查看两个版本的实时测试
1 不出所料,ideone.com 和 stackoverflow 的语法高亮器还没有准备好:)
关于c++ - 如何在 C++ 中打印\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8029783/