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 ...
随机推荐
- python 深入模块和包
模块可以包含可执行语句以及函数的定义. 这些语句通常用于初始化模块. 它们只在 第一次 导入时执行.只在第一次导入的时候执行,第一次.妈蛋的第一次...后面再次导入就不执行了. [1](如果文件以脚本 ...
- WC总结
去了人生中第一次全国WC,在四川绵阳南山中学举行,去了这么一次,感受颇多,不忍心白白地让时间流逝,于是写篇随笔记录一下. 全程,共计8天. [第1天] 签到,拿餐票,看了看讲义,觉得要狗带. 开营仪式 ...
- js生成当前时间
js生成当前时间 var today=new Date(); function itArray(){ this.length=itArray.arguments.length for(var i=0; ...
- Linux之Shell的算术运算
在Bash的算术运算中有以下几种方法:名称 语法 范例算术扩展 $((算术式)) r ...
- Linux的五个查找命令
1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...
- r-cnn学习系列(三):从r-cnn到faster r-cnn
把r-cnn系列总结下,让整个流程更清晰. 整个系列是从r-cnn至spp-net到fast r-cnn再到faster r-cnn. RCNN 输入图像,使用selective search来构造 ...
- 试用vSphere 6(三):安装vCenter 6(独立数据库)之:vCenter安装与配置
------------------------------------------ 一.VMware vSphere 6(RC版)安装配置系列文章: 1.试用vSphere 6(一):安装ESXi ...
- dp重拾-01背包--HDU 2602
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like ...
- JQuery textarea中val(),text()
val()是当前输入框的前台显示内容 text()是原始内容, 调试时浏览器审查元素可以发现如果只改变val(),text()值是不会改变的
- java抓取快递100信息接口
package zeze; import java.io.IOException; import org.json.JSONArray; import org.json.JSONException; ...