如何在C++中打印\"
how to print " in C++
我需要打印一个字符串,确切地说:
std::string("-I"/path/to/dir" ");
基本上,我需要这样做,因为我正在使用c++代码来生成c++代码。
我想通过ofstream写入上面的字符串,所以像
ofstream fout;
fout << the_string << endl;
问题是我不能在字符串内做\"
只需转义斜杠以及引号!即"
—> \"
fout << "std::string("-I\"/path/to/dir\" ");" << std::endl;
在c++ 0 x /c++ 11
fout << R"(std::string("-I"/path/to/dir" ");)" << std::endl;
它使用一个原始字符串<一口> 1 一口>
查看两个版本的实时测试:http://ideone.com/TgtZK
1毫不奇怪,ideone.com和stackoverflow的语法高亮还没有准备好:)
这行得通:
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "std::string("-I\"/path/to/dir\" ");" << endl;
return 0;
}
打印std::string("-I"/path/to/dir" ");
关键是:你需要转义斜杠和引号
我希望我正确理解了你的问题:
Escape and Escape
"
:
\"
您可能想要尝试添加额外的"/"字符,因为单个"/"将不会被解析为字符串。我认为它应该工作(我是Java/c#的家伙,我自己也遇到过这个问题几次)。
这可以作为标题问题的答案,即使它不是你的问题:
#include <iostream>
using namespace std;
int main(){
std::string foo = string(" /!\ WARNING /!\ -> 1");
cout << foo << endl;
cout << " /!\ WARNING /!\ -> 2" << endl;
return 0;
}
输出为:
/! warning/!→1
/! warning/!→2
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 回溯C++不打印函数,因此文件
- 在一定长度后从数组中打印时缺少整数
- 为什么这个 c++ 代码打印出长度 5,当我打印出字符串时,程序会自动终止?
- 在gem5中打印文件中的所有cache_blocks
- 打印数字图案
- Log4cpp:以UTC/GMT时区打印日期
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数