日期:2014-05-16  浏览次数:20731 次

ls -l命令和stat的结果不一致
在linux下发现一个很奇怪的现象,我把几个7月的文件夹(以及文件夹下的文件和目录)tar到一个新的目录下。用ls -l命令看到的文件时间还是7月份的时间。然后我用以下代码去显示解压后的文件的时间,发现用程序显示出的时间却是当前解压缩的时间。
代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <string.h>
int main()
{
  char strFilename[200]="xxxxxxxxxxxxxxxxxxxx";
  struct dirent* pdir;
  char strFile[200];
  DIR* dir;
  time_t tDirCreate;
  struct stat buf;
  dir = opendir(strFilename);
  while(( pdir = readdir(dir)) != NULL)
  {
  if ((strcmp(pdir->d_name,".") != 0) && (strcmp(pdir->d_name,"..") != 0))
  {
  memset(strFile,'\0',sizeof(strFile));
  strcpy(strFile,strFilename);
  strcat(strFile,"/");
  strcat(strFile,pdir->d_name);
  sprintf(strFile,"%s%s",strFile,"/");
  memset(&buf,'\0',sizeof(buf));
  stat(strFile,&buf);
  tDirCreate=buf.st_ctime;
  printf("\niDelFile:%s\n %s %s %s\n",strFile,ctime(&tDirCreate),
  ctime(&buf.st_atime),ctime(&buf.st_mtime));
  }
  }
  closedir(dir);
  exit(1);
}




------解决方案--------------------
tDirCreate=buf.st_ctime; 
它是 time of last status change ,
改成:
tDirCreate=buf.st_mtime
------解决方案--------------------
不是很懂,顶
------解决方案--------------------
time_t st_atime; /* time of lastaccess*/
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last change */

可能还是参数没有选对吧....
------解决方案--------------------
你在解压得时候请加这个参数:
tar zvf --atime-preserve filename.tar
这样,他就会保持原来的时间
------解决方案--------------------
tar的问题. 保留/不保留原时间