sscanf整形输出误区
2016-06-25 11:01
330 查看
#include #include using namespace std; #define USHORT unsigned short typedef struct tagDateInfo { USHORT usYear; USHORT usMonth; USHORT usDay; USHORT usHour; USHORT usMinite; USHORT usSecond; }DATE_INFO_S; int main(int argc, char **argv) { DATE_INFO_S stDateInfo = {11, 11, 11, 11, 11, 11}; void *pcInfo = &stDateInfo; string strSrc("20160615091919"); sscanf(strSrc.c_str(), "%4d", &stDateInfo.usYear); cout << stDateInfo.usYear << stDateInfo.usMonth << stDateInfo.usDay << stDateInfo.usHour << stDateInfo.usMinite << stDateInfo.usSecond << endl; system("pause"); return 0; }
输出结果会发现stDateInfo.usMonth的值发生了变化,是因为当格式化输出为" %d" 、 "%lu "、"%x "等整形时,默认参数是整形,4个字节,所以导致后面的值也被改了。
将成员类型改成unsigned long就可以了。
相关文章推荐
- 新浪微博腾讯微博Android开发笔记
- 用Redis的set指令实现锁
- java小知识点2
- docker 的LXC技术
- 在CentOS上搭建PHP服务器环境
- Oracle 数据库开启关闭 及 端口监听相关命令
- 怎样修改JTree中每级节点前面的图标
- 费用报销-不能走到已付款状态
- ACdream 1210 Chinese Girls' Amusement(高精度)
- Mysql 数据库的数据大小查看:
- 通过ServletContext类的getRealPath()方法获取服务器的真实地址
- window scipy install
- 【Python学习日记】函数式编程 之 高阶函数
- 对01背包问题的思考
- 51nod 1094 和为k的连续区间
- AppNexus
- 自定义View之垂直翻页公告
- Linux下启动相关oracle服务与监听的命令
- C++设计模式浅识建造者模式
- 125. Valid Palindrome