C_and_CPP 版 (精华区)
发信人: fork (撒哈拉沙漠的沙), 信区: C_and_CPP
标 题: Re: 哪位知道C语言中读写文件时sprintf的用法?
发信站: BBS 哈工大紫丁香站 (Wed Jun 9 22:37:22 2004)
sprintf, swprintf
Write formatted data to a string.
int sprintf( char *buffer, const char *format [, argument] ... );
int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );
Routine Required Header Compatibility
sprintf <stdio.h> ANSI, Win 95, Win NT
swprintf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introductio
n.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
sprintf returns the number of bytes stored in buffer, not counting the termina
ting null character. swprintf returns the number of wide characters stored in
buffer, not counting the terminating null wide character.
Parameters
buffer
Storage location for output
format
Format-control string
argument
Optional arguments
For more information, see Format Specifications.
Remarks
The sprintf function formats and stores a series of characters and values in b
uffer. Each argument (if any) is converted and output according to the corresp
onding format specification in format. The format consists of ordinary charact
ers and has the same form and function as the format argument for printf. A nu
ll character is appended after the last character written. If copying occurs b
etween strings that overlap, the behavior is undefined.
swprintf is a wide-character version of sprintf; the pointer arguments to swpr
intf are wide-character strings. Detection of encoding errors in swprintf may
differ from that in sprintf. swprintf and fwprintf behave identically except t
hat swprintf writes output to a string rather than to a destination of type FI
LE.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_stprintf sprintf sprintf swprintf
Example
/* SPRINTF.C: This program uses sprintf to format various
* data and place them in the string named buffer.
*/
#include <stdio.h>
void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;
/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );
printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
Output
Output:
String: computer
Character: l
Integer: 35
Real: 1.732053
character count = 71
Stream I/O Routines
See Also _snprintf, fprintf, printf, scanf, sscanf, vprintf Functions
------------------------------------------------------------------------------
--
Send feedback to MSDN.Look here for MSDN Online resources.
--
女人啊!华丽的金钻闪烁的珠光为你赢得了女皇般虚妄的想象,岂知你的周遭只剩
下势力的毒,傲慢的香,撩人也杀人的芬芳!女人啊!当你再度向财富致敬,向权
利挥手,向名利高举臂膀!请不要询问那只曾歌咏过的画眉,它已不知飞向何方~~~
※ 来源:·哈工大紫丁香 http://bbs.hit.edu.cn·[FROM: 202.102.144.36]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.229毫秒