snprintf 返回值
在平时写代码的过程中,我一个推荐带有n系列的字符串函数,如
strcat ->strncat
sprintf->snprintf
我们有类似的一个函数
void dump_kid(std::string* str, uint32_t kid)
{
char buffer[];
int len;
if (str->empty())
{
len = snprintf(buffer,sizeof(buffer), "%u", kid);
}
else
{
len = snprintf(buffer,sizeof(buffer), ",%u", kid);
}
str->append(buffer, len);
}
我们知道,string的append可以接受没有长度的char*,但这样的效率不高,其内部也会strlen一下。
所以,在此处我们利用了snprintf的返回值,但查了下,snprintf的返回值有个陷阱。
snprintf的函数原型为:
int snprintf(char *str, size_t size, const char *format, …);
说明:
之前以为snprintf的返回值是实际写入到str字符串的长度,其实不然
case 1 : 如果要输出的字符串的长度< size, 主要这里不包括=, 因为snprintf会自动将\0加入到str中,
snprintf的返回值是实际str的长度
case 2 : 如果要输出的字符串长度>= size, 则表明str的长度不够写入原有的长度,则snprintf的返回值
在理想情况下(即str的长度足够长)的字符串长度,所以其返回值可能会出现>= size的情况。
另外需要说明的snprintf会自动将’\0′追加到str的末尾,而snprintf的返回值是不包括’\0′的
这个在官方的manual里写的比较清楚:
If the output was truncated due to this limit then the return
value is the number of characters (not including the trailing ’\0’) which would have been written to the final string if enough space had been available.
Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value
is returned.
而sprintf的说明
On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.
成功返回写字符的总数,其中不包括结尾的null字符。
On failure, a negative number is returned.失败了,返回一个负数。
但说明情况下返回负数呢?特殊情况下,就会缓冲区溢出了,并不能返回负数?
snprintf 返回值的更多相关文章
- snprintf 返回值陷阱 重新封装
snprintf()函数用于将格式化的数据写入字符串,其原型为: int snprintf(char *str, int n, char * format [, argument, ...]); st ...
- snprintf()返回值的陷阱
int snprintf(char *restrict buf, size_t n, const char * restrict format, ...); 函数说明:最多从源串中拷贝n-1个字符到 ...
- socket读写返回值的处理
在调用socket读写函数read(),write()时,都会有返回值.如果没有正确处理返回值,就可能引入一些问题 总结了以下几点 1当read()或者write()函数返回值大于0时,表示实际从缓冲 ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- 由Dapper QueryMultiple 返回数据的问题得出==》Dapper QueryMultiple并不会帮我们识别多个返回值的顺序
异常汇总:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 今天帮群友整理Dapper基础教程的时候手脚快了点,然后遇到了一个小问题,Dapp ...
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- Asp.net中存储过程拖拽至dbml文件中,提示无法获得返回值
Asp.net中存储过程拖拽至dbml文件中,提示无法获得返回值,去属性表中设置这时候会提示你去属性表中更改返回类型. 其实存储过程返回的也是一张表,只不过有时候存储过程有点复杂或者写法不规范的话不能 ...
- SubSonic3.0使用存储过程查询时,不能使用output参数返回值的问题修改
有个群友问SubSonic3.0执行存储过程时能不能使用output参数返回值,说测试过后获取不到返回值,早上有些时间所以就尝试修改了一下 首先在数据库中创建一个存储过程 CREATE PROCEDU ...
- 支持多返回值存储过程的SqlHelper
public readonly string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionS ...
随机推荐
- IOS打包脚本
1.xcodebuild clean -project $projectname.xcodeproj -configuration Release -alltargets2.xcodebuild ar ...
- Dozer应用——类之间值的映射
1. Mappings via Annotation public class SourceBean { private Long id; private String name; @Mapping( ...
- android4.4.2内核移植3.4.1
01◑ 内核源码总目录下Makefile文件修改: 195行: ARCH ?= $(SUBARCH)替换成: ARCH ?= arm 02◑ arch/arm下makefile: ①差7行 # tes ...
- 高难度(3)RenderScript
RenderScript RenderScript is a framework for running computationally intensive tasks at high perform ...
- Android开发之获取系统版本号
获取系统版本号:获取当前系统的版本号: textView.setText("Product Model: " + android.os.Build.MODEL + ",& ...
- add-two-numbers-ii
注意:有一种好的方法,是将链表倒转,然后依次相加. 但是,按照题目要求,用了不改变原链表的方法. 就是将两个链表增加到相同长度,然后递归相加,子函数返回后处理进位. https://leetcode. ...
- Smack IQ包的扩展
前几天一直很烦躁,怎么扩展smack的IQ包堵了我好久,今天静下心来看了下smack的源码,把这个问题解决了.下面给出步骤: 如果我们要扩展一个如下所示的IQ包: <iq id="00 ...
- UVa 11427 (期望 DP) Expect the Expected
设d(i, j)表示前i局每局获胜的比例均不超过p,且前i局共获胜j局的概率. d(i, j) = d(i-1, j) * (1-p) + d(i-1, j-1) * p 则只玩一天就就不再玩的概率Q ...
- LeetCode Number of Islands 岛的数量(DFS,BFS)
题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...
- LeetCode: Reverse Words in a String && Rotate Array
Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...