当使用_snwprintf时,我得到的是汉字而不是英文。
wchar_t outfile[1024];
char const*outf = "test";
_snwprintf(outfile, 1024, L"%s.zip", outf);
_wfopen(outfile, L"wb");
文件名应该是“test”,但在输出文件中它是中文的。
当我这样尝试时没有问题,输出文件包含预期的英语:
_snwprintf(outfile, 1024, L"justtest.zip");
_wfopen(outfile, L"wb");
如何更正第一个代码块,以便在输出文件中正确显示文件名?
最佳答案
wchar_t outfile[1024];
char const*outf = "test";
_snwprintf(outfile, 1024, L"%s.zip", outf);
_wfopen(outfile, L"wb");
outf
是 ANSI (char
),但 outfile
是 UTF-16 (wchar_t
),并且 _snwprintf
需要 wchar_t
。 Visual Studio 2015 发出以下警告:
warning C4477: '_snwprintf' : format string '%s' requires an argument of type 'wchar_t *', but variadic argument 1 has type 'const char *'
您需要更改代码才能使用wchar_t
:
wchar_t outfile[1024];
const wchar_t* outf = L"test";
_snwprintf(outfile, 1024, L"%s.zip", outf);
wprintf(L"%s\n", outfile);
关于c - 使用_snwprintf时意外获取中文字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39736168/