Linux and Windows snprintf区别(zz)
来源:互联网 发布:c语言运算符优先级规律 编辑:程序博客网 时间:2024/05/26 02:20
今天使用snprintf函数时想到了:
strcpy, strncpy
strcmp, strncmp
strcat, strncat
sprintf, snprintf
最喜欢用的就是snprintf, 因为它会自动在后面加'/0'. 在网上看了一下. 原来VC中_snprintf不是这样的.
VC中的_snprintf函数并没有按照这样的规定来做,它在输出缓冲区不够大时就不会输出结尾的'/0'(跟strncpy的行为类似)。所以要让上面的程序工作正常,必须做相应的修改。
char buf[5];
_snprintf(buf, 5, "This is a test string."); // buf becomes "This ", buf[4] is ' '
buf[4] = 0; // buf[4] is '/0' now. 为了安全, 在windows下要手动添加
_snprintf(buf, 6, "This is a test string."); // ERROR: buffer overflow
_snprintf(buf, 5, "abc"); // buf becomes "abc", the value of buf[3] is '/0', buf[4] is undefined.
如果要保证可移植性,就得按VC的写法,做一次对于标准来说很多余的"填0"的操作,再通过定义宏来区别平台分别选用snprintf或_snprintf。
一个小的测试代码:
可以看到有两个地方不一样:
1>. 结尾的'/0'处理!
2>. 函数返回值不一样!
#include <stdio.h>
#include <string.h>
#define STR_LEN 32
#ifdef _WIN32
#define snprintf _snprintf
#endif
/**
* dump string
*/
static void dump_str(const char *title, const char *str, int len)
{
int i;
printf("%s[%d].%p /n", title, len, str);
for (i = 0; i < len; i++)
printf("<%c,%02x>", str[i], (unsigned char)str[i]);
printf("/n");
}
int main(int argc, char **argv)
{
int t;
char str1[STR_LEN], str2[STR_LEN];
strcpy(str1, "123456789");
dump_str("str1", str1, STR_LEN);
t = snprintf(str2, STR_LEN, "%s", str1);
dump_str("str2", str2, STR_LEN);
printf("snprintf() ret.%d /n", t);
t = snprintf(str2, 5, "%s", str1);
dump_str("str2", str2, STR_LEN);
printf("snprintf() ret.%d /n", t);
return (0);
}
/*
Test Env:
gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)
Result:
str1[32].0xbffb9190
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><?fb><?bf><?d5><?82><,04><,08>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00><?c8><?91><?fb><?bf><&,26><?85><,04><,08>
str2[32].0xbffb9170
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><w,77><,01>< ,00><?92><?fb><?bf><?f8><d,64><?ad>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>
snprintf() ret.9
str2[32].0xbffb9170
<1,31><2,32><3,33><4,34>< ,00><6,36><7,37><8,38><9,39>< ,00><w,77><,01>< ,00><?92><?fb><?bf><?f8><d,64><?ad>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>
snprintf() ret.9
-------------------------------------------------------------------------
Test Env:
Microsoft Windows XP [版本 5.1.2600]
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Result:
str1[32].0012FF60
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><,12>< ,00><,10><,1d><@,40>< ,00>< ,00><,08>< ,00>< ,00><,04>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00><?e0><,1e><@,40>< ,00>
str2[32].0012FF3C
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><?93><|,7c><,ff><,ff><,ff><,ff><?eb><,06><?93><|,7c><?ef><0,30><@,40>< ,00>< ,00>< ,00><7,37>< ,00>< ,09>< ,00>< ,00>< ,00>
snprintf() ret.9
str2[32].0012FF3C
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><?93><|,7c><,ff><,ff><,ff><,ff><?eb><,06><?93><|,7c><?ef><0,30><@,40>< ,00>< ,00>< ,00><7,37>< ,00>< ,09>< ,00>< ,00>< ,00>
snprintf() ret.-1
*/
- Linux and Windows snprintf区别(zz)
- Linux and Windows snprintf区别
- Linux and Windows snprintf区别
- Linux and Windows snprintf区别
- linux下和windows 下 snprintf函数的区别
- 为什么snprintf比sprintf更安全?(另外,请注意, Windows和Linux中的snprintf函数有区别)
- 为什么snprintf比sprintf更安全?(另外,请注意, Windows和Linux中的snprintf函数有区别)
- snprintf函数的使用(Windows和Linux下的区别)
- snprintf函数使用(Windows与Linux版本)
- snprintf函数使用(Windows与Linux版本)
- snprintf linux 与 windows下差异
- snprintf函数使用(Windows与Linux版本)
- snprintf函数使用(Windows与Linux版本)
- snprintf linux 与 windows下差异
- snprintf函数使用(Windows与Linux版本)
- linux c之snprintf()和sprintf()区别
- windows socket and linux socket 区别?
- linux转windows的一些小错误:snprintf
- C++读书笔记之抽象
- MySQL优化设置
- Java中hashmap和hashtable的区别
- 需要关注的几个方向
- SQL server数据库连接
- Linux and Windows snprintf区别(zz)
- 记对一个key file crackme的破解
- 分布式 Key-Value 存储系统:Cassandra 入门
- 10号BIOS中断功能详细
- 用if、Exception还是assert?——看JavaEye论坛帖子:《面试题:用Exception异常还是if判断》的感想
- javascript 作用域链
- C# 堆和栈的区别?
- Linux 磁盘管理命令
- C++读书笔记之C++语言文法(C++ Language Grammer)