广角字符印刷单个字符发生了什么变化? 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变量为Han character 坠。根据伊戈尔在this post中的评论,大流看起来有点破碎。
目标最终是输出到由分隔符分隔的宽字符到文件的文件数组。某种方式吗?
答案 0 :(得分:1)
代码页大部分已过时,Unicode取代它。此处的问题与以前一样,尝试在文本/ ANSI模式下打开Unicode文件。
由于您已将其标记为c ++,因此您只需使用标准库std::wstring
和std::wfstream
,即可避免c-string分配的麻烦。
#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"English\n";
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;