#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就可以了。