2012-04-22 197 views
0

我必须知道文件夹中某些文件的修改日期。它可以工作,但不适用于所有类型的文件。 例如,它适用于.c,.txt,但不适用于其他类型,例如.mp4,.jpg和.mp3(我创建的应用程序通常需要处理多媒体文件)。它打印出“无法显示时间”,所以我想问题在于stat()。谢谢。stat()返回错误

这是代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <dirent.h> 
#include <string.h> 
#include <time.h> 
#include <sys/types.h> 
#include <sys/stat.h> 

char parola[12]="", hash[32]="", esadecimale[1000]="", system3[100]="./md5 "; 
int i, len, len2; 
int bytes; 
char cwd[1024]; 

int main(void) 
{ 
char t[100] = ""; 
struct stat b; 
DIR *dp; 
char destinationFolder[100] = "/Users/mattiazeni/Desktop/Prova/"; //Me la passa da sopra 
struct dirent *dir_p; 
dp = opendir(destinationFolder); 
if (dp == NULL) exit(1); 

len = strlen(destinationFolder); 

for (i=0;i<len;i++) { 
    system3[i+6]=destinationFolder[i]; 
} 

while((dir_p = readdir(dp)) != NULL) { 
    if (dir_p -> d_name[0] != '.') { 
     //printf("%s\n", dir_p -> d_name); 
     len2 = strlen(dir_p -> d_name); 
     for (i=0;i<len2;i++) { 
      if (dir_p -> d_name[i] == ' '){ //Mi serve per correggere i nomi dei file con spazi 
       system3[i+len+6]='\\'; 
      } 
      else system3[i+len+6]=dir_p -> d_name[i]; 
     } 
     system(system3); //Passa il valore a md5 che calcola l'hash e lo stampa nel file che ci serve insieme al persorso/nome del file 

     FILE *fp; 
     if((fp=fopen("userDatabase.txt", "ab"))==NULL) { 
      printf("Error while opening the file..\n"); 
      fclose (fp); 
     } 
     else { 
      if (!stat(dir_p -> d_name, &b)) { 
      strftime(t, 100, "%d/%m/%Y %H:%M:%S", localtime(&b.st_mtime));   //C'è ancora qualche errore!! 
      fprintf(fp, "%s", t);   
      } 
      else { 
       perror(0); 
       fprintf(fp, "error"); 
      } 
      fprintf(fp, " initialized"); 
      fprintf(fp, "\n"); 
     } 
     fclose (fp); 
     for (i=len+6;i<len+6+len2;i++) { 
      system3[i]=' '; 
     } 
    } 
} 
closedir(dp); 
return 0; 
} 
+3

使用'perror'而不是“无法显示时间”printf来知道你正在得到什么错误。 – Mat 2012-04-22 15:22:52

回答

3

使用perror()。你也不应该使用st_mtime

stat: 
     On success, zero is returned. 
     On error, -1 is returned, and errno is set appropriately.

99%肯定它是因为dir_p -> d_name不存在,而这又可能是因为本地化问题。

你可以这样做:

fprintf(stderr, 
     "Unable to stat %s\n", 
     dir_p->d_name); 
perror(0); 

也;如果您正在检查文件状态,它不应该是->f_name而不是->d_name? - (除非你使用d_name文件名偏离航向

而且你fclose(fp)是你fp == NULL检查之外。由于您不返回或以其他方式中止流程,因此如果fopen失败,您将面临SIGSEGV风险。


编辑:你用这样的东西得到了什么?

#include <unistd.h> 

char cwd[1024]; 

... 


} else { 
    fprintf(stderr, 
      "Unable to stat '%s'\n", 
      dir_p->d_name); 
    perror(0); 

    if (getcwd(cwd, sizeof(cwd)) == NULL) { 
     perror("getcwd() error"); 
    } else { 
     fprintf(stderr, 
       "in directory '%s'\n", 
       cwd); 
    } 
} 

EDIT2:

首先;我说getcwd() != NULL应该是==。 Se改变。 (对我有害)

代码中的问题。 (还有一些),但关于stat - 您使用readdir的d_name。这是只有文件名;不是dir +文件名。从而;你即:

stat(dir_p->d_name, ...) 

其中即变为:

stat("file.mp4", ...) 

最简单的速战速决(寿脏)为:

/* you need to terminate the system string after your for loop */ 
system3[i + len + 6] = '\0'; 

system(system3); 

if (!stat(system3 + 6, &b)) { 
+0

感谢您的回复。 我使用dir_p - > d_name,因为我扫描目录以便知道哪些文件在其中,然后为每个文件我想知道修改日期。 随着perror我得到“没有这样的文件或目录”。但我不明白为什么,这些文件在那里..还因为在这些文件上,我计算了md5散列,并且它很好,问题只针对日期。 – phcaze 2012-04-22 17:22:04

+0

@phcaze:dir_p-> d_name'打印为什么?也;你可以检查当前的工作目录,如果你做了一些改变。使用上面的新代码。它打印什么? – Morpfh 2012-04-22 17:45:36

+0

dir_p-> d_name使用文件名/U​​sers/user/Desktop/file.mp4打印路径。 使用您的代码,它会在屏幕上打印“/Users/user/Desktop/file.mp4 getcwd()错误:未定义的错误:0”。 – phcaze 2012-04-22 18:07:12

0

您应该使用统计的完整路径() 。统计不知道你是哪个目录兴趣

... 
char bigbuff[PATH_MAX]; 

sprintf(bigbuff, "%s/%s", destinationFolder, dir_p->d_name); 

rc = stat (bigbuff, &b); 
... 
+0

是的,这是问题! :) 谢谢。但为什么它只给一些文件带来错误? – phcaze 2012-04-23 06:29:05

0

这是为了扫描文件的目录最后的工作代码,并将其打印在与修改日期一个txt输出文件:

#include <stdio.h> 
#include <stdlib.h> 
#include <dirent.h> 
#include <string.h> 
#include <time.h> 
#include <sys/types.h> 
#include <sys/stat.h> 

char system3[6]="./md5 "; 

int main(void) 
{ 
char t[100] = ""; 
char bigbuff[200]; 
struct stat b; 
char destinationFolder[100] = "/Users/mattiazeni/Desktop/Prova"; //Me la passa da sopra 
DIR *dp; 
struct dirent *dir_p; 
dp = opendir(destinationFolder); 
if (dp == NULL) exit(1); 
while((dir_p = readdir(dp)) != NULL) { 
    if (dir_p -> d_name[0] != '.') { 
     sprintf(bigbuff, "%s%s/%s",system3, destinationFolder, dir_p->d_name); 
     system(bigbuff); 

     FILE *fp; 
     if((fp=fopen("userDatabase.txt", "ab"))==NULL) { 
      printf("Error while opening the file..\n"); 
      fclose (fp); 
     } 
     else { 
      sprintf(bigbuff, "%s/%s", destinationFolder, dir_p->d_name); 
      if (!stat(bigbuff, &b)) { 
      strftime(t, 100, "%d/%m/%Y %H:%M:%S", localtime(&b.st_mtime));   //C'è ancora qualche errore!! 
      fprintf(fp, "%s", t);   
      } 
      else { 
       perror(0); 
       fprintf(fp, "error"); 
      } 
      fprintf(fp, "\n"); 
     } 
     fclose (fp); 
    } 
} 
closedir(dp); 
return 0; 
} 

谢谢大家的帮助!