sprintf_s的使用
int sprintf_s(char *restrict buffer, rsize_t bufsz,
const char *restrict format, ...);
将数据格式化输出到字符串,sprintf_s()是sprintf()的安全版本,通过指定缓冲区长度来避免sprintf()存在的溢出风险。
sprintf_s原先只有windows的编译器才只支持,并不是C中的标准函数。
在C11标准中加入了对该函数的支持,但是是可选的,并非强制加入。
C11中规定,如果编译器实现了__STDC_LIB_EXT1__ 宏,那么就要支持对该函数的实现。
gcc编译器只是部分的支持C11标准,本人测试在ubuntu的gcc 5.4.0版本中也没有实现__STDC_LIB_EXT1__ 。
gcc中可以用snprintf函数简单替代sprintf_s,但是注意2者在实现上是有一定的区别,不是完全相同。
int snprintf( char *restrict buffer, int bufsz,
const char *restrict format, ... );
C11原文如下:
__STDC_LIB_EXT1__ The integer constant 201ymmL, intended to indicate support
for the extensions defined in annex K (Bounds-checking interfaces).
Implementations that do not define __STDC_LIB_EXT1__ are not required to conform to these
specifications.
C++网站给出的使用建议如下:
As all bounds-checked functions, printf_s, fprintf_s, sprintf_s, and snrintf_s
are only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation
and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including <stdio.h>.
参考用法如下:
#if defined(__STDC_LIB_EXT1__)
#if (__STDC_LIB_EXT1__ >= 201112L)
#define __STDC_WANT_LIB_EXT1__ 1 /* Want the ext1 functions */
#endif
#endif #include <stdlib.h>
#include <string.h>
#include <stdio.h> #if (__STDC_WANT_LIB_EXT1__ == 1)
char tempArray[];
sprintf_s(tempArray, , "Int %d", );
#endif
Windows的中用法如下:
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
sprintf_s(...)
#else
sprintf(...)
#endif
sprintf_s的使用的更多相关文章
- sprintf_s的教训
sprintf_s 是个比sprintf更安全的函数,今天在使用的过程中犯了个错误,代码的大致意思如下 void Test_sprintf_s() { ]; memset(buff, , sizeof ...
- VS2013编译报错error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
解决方法有两个: 1. 在预编译头文件stdafx.h里(在没有include任何头文件之前)定义下面的宏: #define _CRT_SECURE_NO_DEPRECATE 2. 将sprintf函 ...
- sprinf sprintf_s 的用法
函数功能: 将数据格式化输出到字符串 函数原型: int sprintf( char *buffer, const char *format [,argument] ... ) 注意这里的buffer ...
- Qt sprintf_s函数格式化字符串出错
Qt sprintf_s函数格式化字符串出错 问题的出现: 我在VS上用c C++写的跨平台的函数 移植到Qt 上面 出现sprintf_s 函数格式化出错. 开始以为是编码问题 反复查找Qt乱码问 ...
- linux下sprintf_s函数的替代
error code: ]; sprintf_s(buf, , "predicted position:(%3d, %3d)", predict_pt.x, predict_pt. ...
- sprintf、vsprintf、sprintf_s、vsprintf_s、_snprintf、_vsnprintf、snprintf、vsnprintf 函数辨析
看了题目中的几个函数名是不是有点头晕?为了防止以后总在这样的细节里纠缠不清,今天我们就来好好地辨析一下这几个函数的异同. 实验环境: Windows下使用VS2017Linux下使用gcc4.9.4 ...
- sprintf_s函数用法
函数功能:将数据格式化输出到字符串 函数原型: int sprintf_s( char *buffer, size_t sizeOfBuffer, const char *format [, argu ...
- c++中sprintf和sprintf_s的区别
参考:https://blog.csdn.net/qq_37221466/article/details/81140901 sprintf_s是sprintf的安全版本,指定缓冲区长度来避免sprin ...
- sprintf_s() 、sprintf()和printf()区别和用法
转载:https://blog.csdn.net/qq_35608277/article/details/80878802 int sprintf_s(char *buffer,size_t size ...
随机推荐
- css 3d 动画 相关
transform-style: preserve-3d; 设置3D模式 perspective:700px :属性定义 3D 元素距视图的距离,以像素计.该属性允许您改变 3D 元素查看 3D 元素 ...
- storyboard有多个Segue的传递
在项目中需要在一个页面向多个页面传不同的值. 在view2Controller和view3Controller中分别有相应的Str2和Str3 - (void)prepareForSegue:(UIS ...
- hdu1520 树形dp Anniversary party
A - Anniversary party Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- Codeanywhere
停用了一个,试一试这个怎么样. 网速太慢了,还在摸索中,没有放弃这个. 备选为Cloud9
- 用普通用户通过sudo进行启动tomcat时报如下异常
用普通用户通过sudo进行启动tomcat时报如下异常 tomcat user 不在 sudoers 文件中.此事将被报告. 这是由于sudo命令使用root用户执行命令.而处于安全性的考虑,一般不允 ...
- Android开发者必备的42个链接
http://mobile.51cto.com/ahot-426035.htm Android开发者必备的42个链接 下面收集了42个帮助大家学习Android的内容链接,部分内容是面向初学者的,帮助 ...
- ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))
通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...
- (转)JAVA AJAX教程第三章—AJAX详细讲解
现在开始深入AJAX,这里还是按老思路,理论和实践相结合.这章的内容主要是讲解AJAX步骤详解,下一张将会用一个AJAX技术实现页面提示效果的实例来说明AJAX的实现. 一.AJAX步骤详解 AJAX ...
- 【python】类中的super().__init__()
来源:百度知道 python中的super( test, self).__init__()是什么意思? 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后&qu ...
- 【linux】nohup运行守护进程
来源:http://www.cnblogs.com/allenblogs/archive/2011/05/19/2051136.html nohup 命令 用途:不挂断地运行命令. 语法:nohup ...