2012-04-22 195 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; 
} 

謝謝大家的幫助!