将 Unicode 字符存储在.txt文件中的新行中
store unicode characters in a .txt file in new line
如何使用C++将文本文件中的 Unicode 字符存储在单独的行中?我尝试使用n
和r
;还有他们的hex
价值观。但是,在.txt
文件中,n
和r
被读取为垃圾值。
以下是代码片段:
{
std::wofstream wof;
wof.imbue(std::locale(std::locale::empty(), new std::codecvt_utf16<wchar_t,
0x10ffff, std::generate_header>));
wof.open(L"file3.txt");
wof << L"Test.x263a x263c x263d x263e A:B:C, 我一个人来 人人都爱喝可乐。 " << endl;
wof << L"n Another test. x002c x263a 263e" << endl;
wof << L"n 我一个人来。 一个人 我一个人来 人人都爱喝可乐。" << endl;
wof << L" Test x263a x263c x263d x263e 我一个人来 人人都爱喝可乐。 " << endl;
wof << L"n 我一个人来 人人都爱喝可乐。" << endl;
}
如图
所示以二进制形式打开文件,并添加 BOM 标记L"xFEFF"
以指示文件为 UTF-16 (LE( 模式。使用此方法,您需要pubsetbuf
使用L"rn"
作为行尾
std::wofstream wof(L"file3.txt", std::ios::binary);
if(wof)
{
wchar_t buf[128];
wof.rdbuf()->pubsetbuf(buf, 128);
wof << L"xFEFF";
wof << L"Test.x263a x263c x263d x263e A:B:C, 我一个人来 人人都爱喝可乐。rn";
wof << L"Another test. x002c x263a 263ern";
}
相关文章:
- 使用新行和不使用新行读取文件
- 如何在文件和行号中抛出错误
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- (C++) 使用诅咒打印从文件中读入的新行
- 如何控制 clang 格式新行中 lessless 的对齐方式
- 从文件中读取给我额外的新行字符C
- 使用 C++ 中的向量在新行上显示文本文件中的列
- 将 Unicode 字符存储在.txt文件中的新行中
- endl 不会在输出文件中创建新行
- C++将字符串写入文本文件中的行;新行问题不起作用
- C++ 在文本文件中的特定字符后插入新行
- 使用getchar()获取字符并将其保存到每次C 的新行中的文件中
- 将新行字符 () 添加到 CSV 文件中
- 如何跳到文件中的新行并重复循环
- seekp() 位置在文件中新行的开头
- 复制另一个文件时,在新文件中创建新行
- 新行不能处理c++中的文件
- c++从文件中读取用空格和新行分隔的单词
- 如何使用C++的ifstream来使用数据长度而不是新行从文件中读取
- 当另一个进程写入新行时从文件中读取