Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用_snwprintf时意外获取汉字_C_Windows_Visual Studio - Fatal编程技术网

C 使用_snwprintf时意外获取汉字

C 使用_snwprintf时意外获取汉字,c,windows,visual-studio,C,Windows,Visual Studio,使用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"); _wfope

使用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");
outp
是ANSI(
char
),但是
outfile
是UTF-16(
wchar\u t
),并且
\u snwprintf
需要
wchar\u t
。Visual Studio 2015发出以下警告:

您需要更改代码以使用
wchar\t

wchar_t outfile[1024];
const wchar_t* outf = L"test";
_snwprintf(outfile, 1024, L"%s.zip", outf);
wprintf(L"%s\n", outfile);

“我在使用snwprintf时得到了汉字”和“文件名是用汉字写的”-有什么问题??请看,提供一个。问题就在标题中!我每次使用第一个代码时都会得到汉字,但使用第二个代码时会得到正确的字符!我正在寻找一个解决方案来解决第一个代码,以便使用随机文件名。欢迎使用堆栈溢出。您使用的是哪个平台(o/s和编译器)?最有可能的情况是,您似乎正在使用MS Visual Studio在Windows上工作-前缀为下划线的名称与MS处理业务的方式相匹配,而不是与标准C处理业务的方式相匹配。这很重要,因为您必须查看Microsoft的手册页面,而不是试图查看C标准。[…continued…][…continuent…]C标准没有定义函数
\u snwprint()
,而是使用原型
int-swprintf(wchar\u t*restrict s,size\u t n,const wchar\u t*restrict format,…)在
中定义了
swprintf()
包含Microsoft领域中的
snwprintf()
名称所暗示的长度,但不包括函数名中的
n
。也没有标准的C
\u wfopen()
wfopen()
函数。或者,如果您不能方便地将参数更改为宽字符串,可以使用
%hs
指定参数为窄字符串。@HarryJohnston我使用了您的解决方案并解决了问题,谢谢。
wchar_t outfile[1024];
const wchar_t* outf = L"test";
_snwprintf(outfile, 1024, L"%s.zip", outf);
wprintf(L"%s\n", outfile);