wsprintf与printf有哪些差异

pan2008 2003-07-24 01:58:41
wsprintf与printf有哪些差异
引用的库呢
...全文
213 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pan2008 2003-07-25
  • 打赏
  • 举报
回复
sscanf函数是怎么回事
在哪个库中
wsprintf和sprintf分别在哪个库中
new1mm 2003-07-25
  • 打赏
  • 举报
回复
Routine Required Header
sscanf <stdio.h>
swscanf <stdio.h>
sprintf <stdio.h>
swprintf <stdio.h>

int sscanf( const char *buffer, const char *format [, argument ] ... );
int swscanf( const wchar_t *buffer, const wchar_t *format [, argument ] ... );
Parameters
buffer
Stored data
format
Format-control string
argument
Optional arguments
Libraries
All versions of the C run-time libraries.

Return Value
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.

Remarks
The sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function; see scanf for a complete description of format. If copying takes place between strings that overlap, the behavior is undefined.

swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf does not handle multibyte hexadecimal characters. swscanf does not handle Unicode fullwidth hexadecimal or “compatibility zone” characters. Otherwise, swscanf and sscanf behave identically.

Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE Defined
_stscanf swscanf


Example
/* SSCANF.C: This program uses sscanf to read data items
· from a string named tokenstring, then displays them.
*/

#include <stdio.h>
void main( void )
{
char tokenstring[] = “15 12 14...”;
char s[81];
char c;
int i;
float fp;

/* Input various data from tokenstring: */
sscanf( tokenstring, “%s”, s );
sscanf( tokenstring, “%c”, &c );
sscanf( tokenstring, “%d”, &i );
sscanf( tokenstring, “%f”, &fp );

/* Output the data read */
printf( “String = %s\n”, s );
printf( “Character = %c\n”, c );
printf( “Integer: = %d\n”, i );
printf( “Real: = %f\n”, fp );
}

Output
String = 15
Character = 1
Integer: = 15
Real: = 15.000000

maxcai 2003-07-24
  • 打赏
  • 举报
回复
wsprintf Function

--------------------------------------------------------------------------------

The wsprintf function formats and stores a series of characters and values in a buffer. Any arguments are converted and copied to the output buffer according to the corresponding format specification in the format string. The function appends a terminating null character to the characters it writes, but the return value does not include the terminating null character in its character count.

Syntax

int wsprintf( LPTSTR lpOut,
LPCTSTR lpFmt,
...
);
Parameters

lpOut
[out] Pointer to a buffer to receive the formatted output. The maximum size of the buffer is 1024 bytes.
lpFmt
[in] Pointer to a null-terminated string that contains the format-control specifications. In addition to ordinary ASCII characters, a format specification for each argument appears in this string. For more information about the format specification, see the Remarks section.
...
[in] Specifies one or more optional arguments. The number and type of argument parameters depend on the corresponding format-control specifications in the lpFmt parameter.
Return Value

If the function succeeds, the return value is the number of characters stored in the output buffer, not counting the terminating null character.
If the function fails, the return value is less than the length of the expected output. To get extended error information, call GetLastError.



Remarks

Security Alert Using this function incorrectly can compromise the security of your application. The string returned in lpOut is not guaranteed to be NULL-terminated. Also, avoid the %s format -- it can lead to a buffer overrun. If an access violation occurs it causes a denial of service against your application. In the worse case, an attacker can inject executable code. Consider using one of the following alternatives: StringCbPrintf, StringCbPrintfEx, StringCbVPrintf, StringCbVPrintfEx, StringCchPrintf, StringCchPrintfEx, StringCchVPrintf, or StringCchVPrintfEx. You should review Security Considerations: Windows User Interface before continuing.
The format-control string contains format specifications that determine the output format for the arguments following the lpFmt parameter. Format specifications, discussed below, always begin with a percent sign (%). If a percent sign is followed by a character that has no meaning as a format field, the character is not formatted (for example, %% produces a single percent-sign character).

The format-control string is read from left to right. When the first format specification (if any) is encountered, it causes the value of the first argument after the format-control string to be converted and copied to the output buffer according to the format specification. The second format specification causes the second argument to be converted and copied, and so on. If there are more arguments than format specifications, the extra arguments are ignored. If there are not enough arguments for all of the format specifications, the results are undefined.

A format specification has the following form:

%[-][#][0][width][.precision]type

Each field is a single character or a number signifying a particular format option. The type characters that appear after the last optional format field determine whether the associated argument is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). The optional fields control other aspects of the formatting. Following are the optional and required fields and their meanings. Field Meaning
Pad the output with blanks or zeros to the right to fill the field width, justifying output to the left. If this field is omitted, the output is padded to the left, justifying it to the right.
# Prefix hexadecimal values with 0x (lowercase) or 0X (uppercase).
0 Pad the output value with zeros to fill the field width. If this field is omitted, the output value is padded with blank spaces.
width Copy the specified minimum number of characters to the output buffer. The width field is a nonnegative integer. The width specification never causes a value to be truncated; if the number of characters in the output value is greater than the specified width, or if the width field is not present, all characters of the value are printed, subject to the precision specification.
.precision For numbers, copy the specified minimum number of digits to the output buffer. If the number of digits in the argument is less than the specified precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds the specified precision. If the specified precision is 0 or omitted entirely, or if the period (.) appears without a number following it, the precision is set to 1.
For strings, copy the specified maximum number of characters to the output buffer.
type 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.



Note It is important to note that wsprintf uses the C calling convention (_cdecl), rather than the standard call (_stdcall) calling convention. As a result, it is the responsibility of the calling process to pop arguments off the stack, and arguments are pushed on the stack from right to left. In C-language modules, the C compiler performs this task.
To use buffers larger than 1024 bytes, use _snwprintf. For more information, see the documentation for the C run-time library.

Behard 2003-07-24
  • 打赏
  • 举报
回复
wsprintf 是支持宽字节,不过也支持单字节的

-----------------------------------
现在混水园了!
snowman_pc 2003-07-24
  • 打赏
  • 举报
回复
wsprintf()打印宽字符,2字节,例如Unicode

Behard 2003-07-24
  • 打赏
  • 举报
回复
1. 应该是 wsprintf 与 sprintf 的比较吧
2. wsprintf 输出为你指定的缓冲区(就是第一个参数),而 printf 则是 stdout(屏幕)
3. wsprintf 是多线程安全函数,而对于的 sprintf 则不是
new1mm 2003-07-24
  • 打赏
  • 举报
回复
1.他的结果不是送往输出设备,而是字符串;
2.那个字符串是由宽字符(2byte)构成
new1mm 2003-07-24
  • 打赏
  • 举报
回复
1.他的结果不是送往输出设备,而是字符串;
2.那个字符串是由宽字符(2byte)构成
idontlikenickname 2003-07-24
  • 打赏
  • 举报
回复


去看看MSDN~

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧