windows 我如何在我的程序中使用 _stprintf,有无 UNICODE 支持?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/594799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use _stprintf in my programs, with and without UNICODE support?
提问by Steve Hanov
Microsoft's <tchar.h> defines _stprintf
as swprintf
if _UNICODE
is defined, and sprintf
if not. But these functions take different arguments! In swprintf
, the second argument is the buffer size, but sprintf
doesn't have this.
Microsoft 的 <tchar.h> 定义_stprintf
为swprintf
if_UNICODE
已定义,sprintf
如果未定义。但是这些函数采用不同的参数!在 中swprintf
,第二个参数是缓冲区大小,但sprintf
没有这个。
Did somebody goof? If so, this is a big one. How can I use _stprintf
in my programs, and have them work with and without _UNICODE
?
有人偷懒吗?如果是这样,这是一个很大的问题。我如何_stprintf
在我的程序中使用,并让它们在有和没有的情况下工作_UNICODE
?
回答by MSalters
You're seeing parallel evolution here. swprintf
is a latecomer to standard C, after it was discovered that (A) 8 bits is insufficient for text and (B) you should pass buffer sizes along with buffers. TCHAR
is a microsoft idea to unify ASCII and Unicode APIs. They dropped the ball, missing point (B). The proper TCHAR
solution should have been to define _stprintf
as either swprintf
or snprintf
.
你在这里看到了平行进化。swprintf
是标准 C 的后来者,在发现 (A) 8 位不足以容纳文本并且 (B) 您应该将缓冲区大小与缓冲区一起传递。TCHAR
是微软统一 ASCII 和 Unicode API 的想法。他们丢球,丢了点 (B)。正确的TCHAR
解决方案应该是定义_stprintf
为swprintf
or 或snprintf
。
The solution is then to simply wrap <tchar.h>
and do this yourself.
解决方案是简单地包装<tchar.h>
并自己完成。
回答by dirkgently
these functions take different arguments!
这些函数采用不同的参数!
There are two different versions available with MS compilers. Take a look here. This is in keeping with the ANSI standard. But I think that does not answer your question. I'll skip it for a while and rather tell you how you can have uniformity.
MS 编译器有两个不同的版本。看看这里。这符合 ANSI 标准。但我认为这并不能回答你的问题。我将略过一段时间,而是告诉您如何获得统一性。
have them work with and without _UNICODE?
他们是否可以使用和不使用_UNICODE?
You are better off using the 'safe string functions' as per MS recommendations. See this. Use `_stprintf_s' and I think you will get around your problem.
根据 MS 的建议,您最好使用“安全字符串函数”。看到这个。使用`_stprintf_s',我想你会解决你的问题。
Did somebody goof?
有人偷懒吗?
EDITED:I don't think so. I don't have the Rationale handy to give you theanswer. I'll post an update when I get my hands on something more concrete. In the meantime look at MSalters' explanation.
编辑:我不这么认为。我没有理由得心应手给你的答案。当我得到更具体的东西时,我会发布更新。同时看看MSalters的解释。
A curious thing is MS's C runtime does notclaim compatibility with the ISO standard.
奇怪的是,MS 的 C 运行时并没有声称与 ISO 标准兼容。
Disclaimer: I am not defending the giant of Redmond, only pointing out stuff that strikes me as odd!
免责声明:我不是在为雷德蒙德的巨人辩护,只是指出让我觉得奇怪的东西!
回答by hunter
This may not be directly answering the question, but one alternative is to use _stprintf_s
. You have to add the extra parm, but then it will still compile both ways, and is more future-proof.
这可能不能直接回答问题,但一种替代方法是使用 _stprintf_s
. 你必须添加额外的参数,但它仍然会以两种方式编译,并且更适合未来。