swprintf 和 fwprintf 和 %c 格式
swprintf and fwprintf and %c format
宽字符打印f单个字符发生了什么变化?VS10 和 MCBS:
#include<stdio.h>
#include <windows.h>
int const maxPathFolder = MAX_PATH - 3;
wchar_t const *delims = L"T";
wchar_t *testString = L"Codepage is: ";
int main()
{
FILE *stream = NULL;
UINT CP = GetConsoleOutputCP();
wchar_t *testName= (wchar_t *)calloc(maxPathFolder, sizeof(wchar_t));
wcscat_s(testName, maxPathFolder, L"C:\printemp.txt");
stream = _wfopen(testName, L"w");
if (fwprintf(stream, L"%s%i%c", testString, CP, delims) == EOF) wprintf(L"Problems writing to File.");
fclose (stream);
swprintf (testName, L"%s%i%c", testString, CP, delims);
free (testName);
}
printemp.txt 中的输出是 Codepage is: 850?
的,swprintf'd testName
中的 delims 变量是汉字坠。根据伊戈尔在这篇文章中的评论,宽阔的溪流看起来有点破碎。
最终目标是将宽字符输出到文件数组,由分隔符分隔。有办法解决它?
代码页大部分已经过时,Unicode取代了它。这里的问题与以前相同,尝试以文本/ANSI模式打开Unicode文件。
由于您已经将其标记为c ++,因此您可以只使用标准库,std::wstring
和std::wfstream
,避免C字符串分配的麻烦。
#include <iostream>
#include <fstream>
#include <string>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int main()
{
//optional: for non-local languages on console
_setmode(_fileno(stdout), _O_U16TEXT);
//write to file (overwrite old file if any)
wchar_t wbuf[128];
std::wofstream fout(L"path.txt", std::ios::binary);
if (fout)
{
fout.rdbuf()->pubsetbuf(wbuf, 128);
fout << L"ελληνικάn";
fout << L"Englishn";
fout << 123 << "n";
fout.close();
}
std::wifstream fin(L"path.txt", std::ios::binary);
if (fin)
{
fin.rdbuf()->pubsetbuf(wbuf, 128);
std::wstring wstr;
while (getline(fin, wstr, L'n')) std::wcout << wstr << L"n";
fin.close();
}
return 0;
}
要与记事本等其他软件兼容,您必须在文件开头添加字节顺序标记:
fout << L"xFEFF";
然后在读取文件时跳过第一个字符(前 2 个字节)。
如果std::wstring
不是一个选项,则使用 new
/delete
运算符而不是 malloc
。
wchar_t *testName = new wchar_t[MAX_PATH];
...
delete[] testName;
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何将strftime中的格式错误作为异常捕获
- 将CHW格式的浮点向量转换为cv::Mat
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 如何以叮当格式设置评论的行长?
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- swprintf 和 fwprintf 和 %c 格式