swprintf 和 fwprintf 以及 %c 格式
swprintf and fwprintf and %c format
wide char printf单字符怎么了? 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 中的 delims 变量 testName
是 Han character 坠. According to Igor's comments in this post,宽流看起来有点破。
最终目的是将宽字符数组输出到文件,文件由分隔符分隔。绕过它?
代码页大部分已经过时,Unicode 取代了它。这里的问题和之前一样,试图以Text/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;
wide char printf单字符怎么了? 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 中的 delims 变量 testName
是 Han character 坠. According to Igor's comments in this post,宽流看起来有点破。
最终目的是将宽字符数组输出到文件,文件由分隔符分隔。绕过它?
代码页大部分已经过时,Unicode 取代了它。这里的问题和之前一样,试图以Text/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;