我写了这个函数。在调试时,我看到在函数的最后,它进入string cmonth[]
声明,然后回到函数的最后,然后返回string cmonth[]
声明,大约 10 次。然后它回到函数的第一行,然后回到函数的最后一行大约 100 次或更多。
int CheckLastDate(string file)
{
string line, dline[200];
int i = 0;
regex rxdate("[[:digit:]].:[[:digit:]].:[[:digit:]].");
ifstream infile;
infile.open(file.c_str());
if(! infile.is_open()) return -1;
while (infile.good())
{
getline(infile, line);
if(regex_search(line, rxdate))
{
dline[i] = line;
i++;
}
}
i--; //needed b/c dline starts at 0;
infile.close();
int imonth, day, hour, min, sec, year;
string month, ampm;
string cmonth[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
month = dline[i].substr(5,3);
//convert month to number
for(int j = 0; j<12; j++)
{
if(month == cmonth[j]) imonth=j+1;
}
day = atoi(dline[i].substr(9, 2).c_str());
hour = atoi(dline[i].substr(12, 2).c_str());
min = atoi(dline[i].substr(15, 2).c_str());
sec = atoi(dline[i].substr(18, 2).c_str());
ampm = dline[i].substr(21, 2);
year = atoi(dline[i].substr(24, 4).c_str());
if(ampm == "PM" && hour != 12) { hour += 12; } //turn into 24 hours
else if(ampm == "AM" && hour == 12) { hour = 0; }
time_t now, dif; //dif = date in file
double diff;
time(&now);
struct tm * timeinfo;
timeinfo = localtime(&now);
timeinfo->tm_mon = imonth - 1;
timeinfo->tm_mday = day;
timeinfo->tm_hour = hour;
timeinfo->tm_min = min;
timeinfo->tm_sec = sec;
timeinfo->tm_year = year - 1900;
timeinfo->tm_isdst = -1; //-1 = no info
dif = mktime(timeinfo);
diff = difftime(now, dif);
if(diff >= 86400) return 1; //more then 24 hours
else return 0;
}
有什么问题吗,还是 C++ 是如何工作的?感谢您的帮助。