[英]Should I use wsprintf() to print a double as a wide string?
I am unable to print double
value using wsprintf()
. 我无法使用
wsprintf()
打印double
值。 I tried sprintf()
and it worked fine. 我试过
sprintf()
,它工作正常。
Syntax used for wsprintf()
and sprintf()
is as follows: 用于
wsprintf()
和sprintf()
语法如下:
wsprintf(str,TEXT("Square is %lf "),iSquare); // Does not show value
sprintf(str," square is %lf",iSquare); // works okay
Am I making any mistakes while using wsprintf()
? 我在使用
wsprintf()
时wsprintf()
?
wsprintf
doesn't support floating point. wsprintf
不支持浮点。 The mistake is using it at all. 错误就是使用它。
If you want something like sprintf
, but for wide characters/strings, you want swprintf
instead. 如果你想要像
sprintf
这样的东西,但对于宽字符/字符串,你需要swprintf
。
Actually, since you're using the TEXT
macro, you probably want _stprintf
instead though: it'll shift from a narrow to wide implementation in sync with the same preprocessor macros as TEXT
uses to decide whether the string will be narrow or wide. 实际上,由于你正在使用
TEXT
宏,你可能会想要_stprintf
:它将从窄到宽的实现转换为与TEXT
用来决定字符串是窄还是宽的相同的预处理器宏。 This whole approach, however, is largely a relic from the days when Microsoft still sold and supported versions of Windows based on both the 32-bit NT kernel, and on the 16-bit kernel. 然而,整个方法在很大程度上是微软仍然出售和支持基于32位NT内核和16位内核的Windows版本的时代遗留下来的。 The 16-bit versions had only extremely minimal wide-character support, so Microsoft worked hard at allowing a single source code base to be compiled to use either narrow characters (targeting 16-bit kernels) or wide characters (to target the 32-bit kernels).
16位版本只有极少的宽字符支持,因此Microsoft努力允许编译单个源代码库以使用窄字符(以16位内核为目标)或宽字符(以32位为目标)内核)。 The 16-bit kernels have been gone for long enough that almost nobody really has much reason to support them any more.
16位内核已经消失了很长时间,几乎没有人真正有理由再支持它们了。
For what it's worth: wsprintf
is almost entirely a historic relic. 值得一
wsprintf
是: wsprintf
几乎完全是一个历史遗迹。 The w
apparently stands for Windows
. w
显然代表Windows
。 It was included as part of Windows way back when (back to the 16-bit days). 它被包含在Windows的一部分中(当时回到16位日)。 It was written without support for floating point because at that time, Windows didn't use any floating point internally--this is part of why it has routines like MulDiv built-in, even though doing (roughly) the same with floating point is quite trivial.
它是在没有浮点支持的情况下编写的,因为当时Windows并没有在内部使用任何浮点数 - 这就是为什么它有像MulDiv内置的例程的一部分,即使用浮点数做(大致)相同也是如此。很琐碎。
wsprintf
does not support floating point. wsprintf
不支持浮点。 See its documentation - lf
is not listed as a valid format code. 请参阅其文档 -
lf
未列为有效的格式代码。
The swprintf function part of the Visual Studio standard library is what you want. Visual Studio标准库的swprintf函数部分是您想要的。 It supports all of the format codes that
sprintf
does. 它支持
sprintf
执行的所有格式代码。
大概你没有编译成UNICODE而TEXT只是#defined而只是一个常规字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.