您的位置:首页 > Web前端

scanf和sprintf函数用法总结

2011-02-21 16:12 363 查看
转自http://shaoweicai.wordpress.com/2010/01/08/sprintf%e5%92%8csscanf%e7%94%a8%e6%b3%95%e8%a7%a3%e6%9e%90/
一,sprintf
sprintf函数的说明转自:http://fogblog.blogbus.com/logs/1437347.html
1:
sprintf 最常见的应用之一莫过于把整数打印到字符串中,所以,spritnf
在大多数场合可以替代itoa。
这样,一个整数的16 进制字符串就很容易得到,但我们在打印16 进制内容
时,通常想要一种左边补0 的等宽格式,那该怎么做呢?很简单,在表示宽
度的数字前面加个0 就可以了。

sprintf(s, "%08X", 4567); //产生:"000011D7"

上面以”%d”进行的10 进制打印同样也可以使用这种左边补0 的方式。

这里要注意一个符号扩展的问题:比如,假如我们想打印短整数(short)-1
的内存16 进制表示形式,在Win32 平台上,一个short 型占2 个字节,所
以我们自然希望用4 个16 进制数字来打印它:

short si = -1;

sprintf(s, "%04X", si);

产生“FFFFFFFF”,怎么回事?因为spritnf 是个变参函数,除了前面两个
参数之外,后面的参数都不是类型安全的,函数更没有办法仅仅通过一个
“%X”就能得知当初函数调用前参数压栈时被压进来的到底是个4 字节的整
数还是个2 字节的短整数,所以采取了统一4 字节的处理方式,导致参数压
栈时做了符号扩展,扩展成了32 位的整数-1,打印时4 个位置不够了,就
把32 位整数-1 的8 位16 进制都打印出来了。如果你想看si 的本来面目,
那么就应该让编译器做0 扩展而不是符号扩展(扩展时二进制左边补0 而不
是补符号位):

sprintf(s, "%04X", (unsigned short)si);

就可以了。或者:

unsigned short si = -1;
sprintf(s, "%04X", si);

2:

浮点数的打印和格式控制是sprintf 的又一大常用功能,浮点数使用格式符”
%f”控制,默认保留小数点后6 位数字,比如:

sprintf(s, "%f", 3.1415926); //产生"3.141593"

但有时我们希望自己控制打印的宽度和小数位数,这时就应该使用:”
%m.nf”格式,其中m 表示打印的宽度,n 表示小数点后的位数。比如:

sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

sprintf(s, "%-10.3f", 3.1415626); //产生:"3.142 "

sprintf(s, "%.3f", 3.1415626); //不指定总宽度,产生:"3.142"

注意一个问题,你猜

int i = 100;

sprintf(s, "%.2f", i);

会打出什么东东来?“100.00”?对吗?自己试试就知道了,同时也试试下面这个:

sprintf(s, "%.2f", (double)i);

第一个打出来的肯定不是正确结果,原因跟前面提到的一样,参数压栈时调
用者并不知道跟i相对应的格式控制符是个”%f”。而函数执行时函数本身
则并不知道当年被压入栈里的是个整数,于是可怜的保存整数i 的那4 个字
节就被不由分说地强行作为浮点数格式来解释了,整个乱套了。

3:

连接字符串

sprintf 的格式控制串中既然可以插入各种东西,并最终把它们“连成一
串”,自然也就能够连接字符串,从而在许多场合可以替代strcat,但
sprintf 能够一次连接多个字符串(自然也可以同时在它们中间插入别的内
容,总之非常灵活)。比如:

char* who = "I";

char* whom = "CSDN";

sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. "

还可以是各种数据类型的整合,如
char cfile_name[100];
 sprintf(cfile_name,"%d%s",v_count,"model.txt");
strcat 只能连接字符串(一段以’’结尾的字符数组或叫做字符缓冲,
null-terminated-string),

但有时我们有两段字符缓冲区,他们并不是以’’结尾。比如许多从第三
方库函数中返回的字符数组,从硬件或者网络传输中读进来的字符流,它们
未必每一段字符序列后面都有个相应的’’来结尾。如果直接连接,不管
sprintf 还是strcat 肯定会导致非法内存操作,strncat 也至少要求第
一个参数是个null-terminated-string,那该怎么办呢?我们自然会想起前
面介绍打印整数和浮点数时可以指定宽度,字符串也一样的。比如:

char a1[] = {’A', ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’};

char a2[] = {’H', ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’};

如果:

sprintf(s, "%s%s", a1, a2); //Don’t do that!

十有八九要出问题了。是否可以改成:

sprintf(s, "%7s%7s", a1, a2);

也没好到哪儿去,正确的应该是:

sprintf(s, "%.7s%.7s", a1, a2);//产生:"ABCDEFGHIJKLMN"

这可以类比打印浮点数的”%m.nf”,在”%m.ns”中,m 表示占用宽度(字
符串长度不足时补空格,超出了则按照实际宽度打印),n 才表示从相应的
字符串中最多取用的字符数。通常在打印字符串时m 没什么大用,还是点号
后面的n 用的多。自然,也可以前后都只取部分字符:

sprintf(s, "%.6s%.5s", a1, a2);//产生:"ABCDEFHIJKL"

(因为sprintf函数将输出写入到字符串s中,并以”结束,所以生成的s
中有”,所以可以用printf(s),而不用担心会出错)

在许多时候,我们或许还希望这些格式控制符中用以指定长度信息的数字是
动态的,而不是静态指定的,因为许多时候,程序要到运行时才会清楚到底
需要取字符数组中的几个字符,这种动态的宽度/精度设置功能在sprintf
的实现中也被考虑到了,sprintf 采用”*”来占用一个本来需要一个指定
宽度或精度的常数数字的位置,同样,而实际的宽度或精度就可以和其它被
打印的变量一样被提供出来,于是,上面的例子可以变成:

sprintf(s, "%.*s%.*s", 7, a1, 7, a2);

或者:

sprintf(s, "%.*s%.*s", sizeof(a1), a1, sizeof(a2), a2);

实际上,前面介绍的打印字符、整数、浮点数等都可以动态指定那些常量值,
比如:

sprintf(s, "%-*d", 4, ‘A’); //产生"65 "

sprintf(s, "%#0*X", 8, 128); //产生"0X000080","#"产生0X

sprintf(s, "%*.*f", 10, 2, 3.1415926); //产生" 3.14"

二, sscanf
MSDN 中sscanf的使用说明

sscanf, swscanf
Read formatted data from a string.

int sscanf( const char *buffer, const char *format [, argument ] … );

int swscanf( const wchar_t *buffer, const wchar_t *format [, argument ] … );

Routine Required Header Compatibility 
sscanf <stdio.h> ANSI, Win 95, Win NT 
swscanf <stdio.h> or <wchar.h> ANSI, Win 95, Win NT 

For additional compatibility information, see Compatibility in the Introduction.

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

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.

Parameters

buffer

Stored data

format

Format-control string

argument

Optional arguments

For more information, see Format Specifications.

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 & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_stscanf sscanf sscanf 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 );

   char  typestring[] = "15, 12; 14.1";
   sscanf( typestring, "%s, %d; %f", s, &c, &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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息