读书人

wsprintf的有关问题

发布时间: 2012-03-20 14:01:11 作者: rapoo

wsprintf的问题?
如何在格式化字串中加入浮点类型数据? 如
TCHAR str[88];
float temp = 3.1415;
wsprintf(str, L "%f ", temp);

这样得到的str的内容是字符串 "f ", 而不是想得到的 "3.1415 "字串.
怎样在wsprintf中格式化浮点数为一个字串?

[解决办法]
用sprintf即可
[解决办法]
ls正解.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char str[88];
float temp = 3.1415;
sprintf(str, "%f ", temp);
printf( "%s ",str);
getch();
return 0;
}
[解决办法]
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define UNICODE
int main()
{
TCHAR str[100];
float f=3.1415926f;
_sntprintf(str,100,TEXT( "%f "),f);
MessageBox(NULL,str,TEXT( "测试wsprintf "),MB_OK);
return 0;
}
[解决办法]
wsprintf 不支持该格式化浮点型

它支持如下格式化字符
Output the corresponding argument as a character, a string, or a number. This field can be any of the following values. Value Meaning
c Single character. This value is interpreted as type WCHAR if the calling application defines Unicode and as type __wchar_t otherwise.
C Single character. This value is interpreted as type __wchar_t if the calling application defines Unicode and as type WCHAR otherwise.
d Signed decimal integer. This value is equivalent to i.
hc, hC Single character. The wsprintf function ignores character arguments with a numeric value of zero. This value is always interpreted as type __wchar_t, even when the calling application defines Unicode.
hd Signed short integer argument.
hs, hS String. This value is always interpreted as type LPSTR, even when the calling application defines Unicode.
hu Unsigned short integer.
i Signed decimal integer. This value is equivalent to d.
lc, lC Single character. The wsprintf function ignores character arguments with a numeric value of zero. This value is always interpreted as type WCHAR, even when the calling application does not define Unicode.
ld Long signed integer. This value is equivalent to li.
li Long signed integer. This value is equivalent to ld.
ls, lS String. This value is always interpreted as type LPWSTR, even when the calling application does not define Unicode. This value is equivalent to ws.
lu Long unsigned integer.
lx, lX Long unsigned hexadecimal integer in lowercase or uppercase.
p Windows 2000/XP: Pointer. The address is printed using hexadecimal.
s String. This value is interpreted as type LPWSTR when the calling application defines Unicode and as type LPSTR otherwise.
S String. This value is interpreted as type LPSTR when the calling application defines Unicode and as type LPWSTR otherwise.
u Unsigned integer argument.
x, X Unsigned hexadecimal integer in lowercase or uppercase

[解决办法]
你可以用sprintf来实现此目的
sprintf(str, "%f ",3.1415);

读书人网 >C语言

热点推荐