这是我尝试获取时间的代码片段:
void GetDate(int &month,int &day,int &year){
time_t SystemTime;
struct tm *OSTime;
OSTime=localtime(&SystemTime);
month=OSTime->tm_mon;
day=OSTime->tm_mday;
year=OSTime->tm_year;
month+=1;
year+=1900;
}
当它到达:
month=OSTime->tm_mon;
它抛出:
Unhandled exception at 0x01101123 in GonzalesP2.exe: 0xC0000005: Access violation reading location 0x00000010.
这是调用 GetDate 的方法:
void Calendar::SetMonthYear(int mon,string sYr){
string temp;
GetDate(tMonth,tDay,tYear);
if(mon==0 && sYr=="0000"){
mon=tMonth;
sYr=tYear;
}
month=mon;
//get the year
temp=sYear.at(2);
temp+=sYear.at(3);
year=atoi(temp.c_str());
//get the century
temp=sYear.at(0);
temp+=sYear.at(1);
cent=atoi(temp.c_str());
FillMonthGrid();
}
请帮忙。
最佳答案
你又忘记了一步:
time (&SystemTime); //^^Should first do this
struct tm *OSTime;
OSTime=localtime(&SystemTime);
您可以在此处找到示例用法:localtime , 在调用 localtime
之前调用 time
。
关于c++ - Access violation reading location 尝试获取系统时间时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951895/