遍历目录时,无法获取文件的正确时间
对于当前目录下的文件,可以得到正确的文件时间。其它目录,得到的文件时间不对,不知道为什么。
string get_date_time(long t)
{
char datetime[32];
struct tm *newtime;
string ret;
if(t == 0)
{
return "";
}
newtime = localtime(&t);
sprintf(datetime,"%04d-%02d-%02d %02d:%02d:%02d",
newtime->tm_year+1900,
newtime->tm_mon+1,
newtime->tm_mday,
newtime->tm_hour,
newtime->tm_min,
newtime->tm_sec);
ret = datetime;
return ret;
}
int search_folder(string path)
{
DIR *pdir;
struct dirent*pentry;
struct statm_stat;
pdir = opendir(path.c_str());
if(pdir == NULL)
{
return ret_invalid_handle;
}
while((pentry = readdir(pdir)) != NULL)
{
if(strcmp(pentry->d_name,".")==0 || strcmp(pentry->d_name,"..")==0)
{
continue;
}
if(pentry->d_type == DT_DIR)
{
search_folder(path+"/"+pentry->d_name);
}
else if(pentry->d_type == DT_REG || pentry->d_type == DT_UNKNOWN) //cygwin下,有系统属性的文件会被识别为DT_UNKNOWN类型
{
lstat(pentry->d_name,&m_stat);
printf("%s %s\n",get_date_time(m_stat.st_mtime).c_str(),pentry->d_name);
}
}
closedir(pdir);
return ret_ok;
}