欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

sscanf_s函数

发布时间:2023/12/9 编程问答 62 豆豆
生活随笔 收集整理的这篇文章主要介绍了 sscanf_s函数 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
sscanf的安全版本sscanf_s的函数定义: int sscanf_s( const char *buffer, const char *format [, argument ] ... );

在msdn中有如下mark,一定要仔细看哟,特别注意红色的部分,否则会出错的,我就在这里折腾了大半天才明白的

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

总结

以上是生活随笔为你收集整理的sscanf_s函数的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。