如何将箭头符号写入文件,然后再将其读回
How do I write arrow symbol to a file and then read it back?
引用http://www.fileformat.info/info/unicode/char/2192/index.htm说右箭头在unicode字符集中是0x2192,所以我尝试用几种不同的方式将值写入文件:
#include <fstream>
using namespace std;
int main()
{
ofstream out("out.txt");
wofstream wout("wout.txt");
out << '0x2192' << endl;
out << 'u2192' << endl;
out << L'u2192' << endl;
out << u'u2192' << endl;
wout << '0x2192' << endl;
wout << 'u2192' << endl;
wout << L'u2192' << endl;
wout << u'u2192' << endl;
return 0;
}
它只打印数字,没有箭头符号。我做错了什么?PS我还想稍后读回这个字符。提前谢谢。
经过一些修改:
#include <fstream>
#include <locale>
using namespace std;
int main() {
// This is the real trick, make the wfostream to print wide characters as
// UTF-8
// what we're going to do is to create a locale that has the ctype category
// copied from the "en_US.UTF-8"
std::locale loc=std::locale(std::locale(),"en_US.UTF8",std::locale::ctype);
ofstream out("out.txt");
// and now add the locale to the stream
out.imbue(loc);
wofstream wout("wout.txt");
// and now add the locale to the stream
wout.imbue(loc);
//out << '0x2192' << endl; // character constant too long (did not compile on g++)
//out << 'u2192' << endl; // character constant too long (did not compile on g++)
out << L'u2192' << endl; // prints 8594
out << u'u2192' << endl; // prints 8594
out << (wchar_t) L'u2192' << endl; // prints 8594
out << (wchar_t) u'u2192' << endl; // prints 8594
//wout << '0x2192' << endl; // character constant too long
//wout << 'u2192' << endl; // character constant too long
wout << 8594 << endl; // prints 8594
wout << L'u2192' << endl; // prints ->
wout << u'u2192' << endl; // prints 8594
wout << (wchar_t) 8594 << endl; // prints ->
wout << (wchar_t) L'u2192' << endl; // prints ->
wout << (wchar_t) u'u2192' << endl; // prints ->
return 0;
}
以上是在Ubuntu linux上执行的,并使用g++编译
相关文章:
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 如何从文件中读取值,然后使用它们来初始化 2D 数组C++
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 按单词搜索文件,然后在C++中打印特定行数
- 将整个二进制文件读入缓冲区,然后以特定格式解析
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何将 id 写入文件,然后将其读回?
- 从文本文件读取,然后将项目存储到列表中
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 使用函数打开文件,然后让其他函数利用该文件?
- 如何添加mp3文件作为资源,然后播放
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 如何将文件逐行读取到矢量中,然后打印矢量
- 如何将QGraphicsItem QList保存到文件中,然后读取它?Qt c++
- 是否可以将前向声明和常规声明合并到一个文件中,然后像分开一样使用它?
- 从文件中找出字符'a',然后计算字符出现的行数
- 汇总然后平均 txt 文件中每 10 行一次
- 从文件中读取并将其内容放入对象数组中,然后对它们进行排序