您的位置:首页 > 其它

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就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: