sscanf_s函数

sscanf的安全版本sscanf_s的函数定义:
int sscanf_s( const char *buffer, const char *format [, argument ] ... );

在msdn中有如下mark,一定要仔细看哟,特别注意红色的部分,否则会出错的,我就在这里折腾了大半天才明白的将sscanf转换为sscanf_s需要指定取值的大小

The sscanf_s function reads data from buffer into the location given by each argument. The arguments after the format string specify pointers to variables with a type that corresponds to a type specifier in format. Unlike the less secure version sscanf_sa buffer size parametersizeOfBuffer is required when using the type field characters c, C, s, S and [. This parameter must be supplied as an additional parameter after each buffer which requires it.

也可以看下面这个例子的,注意红色的部分

// crt_sscanf_s.c
// This program uses sscanf_s to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>

int main( void )
{
   char  tokenstring[] = "15 12 14...";
   char  s[81];
   char  c;
   int   i;
   float fp;

   // Input various data from tokenstring:
   // max 80 character string plus NULL terminator
   sscanf_s( tokenstring, "%s", s, sizeof(s) );
   sscanf_s( tokenstring, "%c", &c, sizeof(char) );

   sscanf_s( tokenstring, "%d", &i );
   sscanf_s( tokenstring, "%f", &fp );

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

Output
String = 15 Character = 1 Integer: = 15 Real: = 15.000000
fscanf_s,fwscanf_s用法和sscanf_s差不多,只是把字符串换成了打开的流。
转自 http://blog.sina.com.cn/s/blog_670e606a0100m1d1.html
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值