首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >stat()返回错误

stat()返回错误
EN

Stack Overflow用户
提问于 2012-04-22 23:15:04
回答 2查看 4.5K关注 0票数 0

我必须知道文件夹中某些文件的修改日期。它可以工作,但并不适用于所有类型的文件。例如,它适用于.c、.txt,但不适用于其他类型,如.mp4、.jpg和.mp3 (我正在创建的应用程序通常必须处理多媒体文件)。它输出"Cannot display the time.",所以我认为问题出在stat()上。谢谢。

代码如下:

代码语言:javascript
复制
#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;
}
EN

回答 2

Stack Overflow用户

发布于 2012-04-23 02:51:20

您应该使用stat()的完整路径名。Stat不知道您感兴趣的是哪个目录。

代码语言:javascript
复制
... 
char  bigbuff[PATH_MAX];

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

rc = stat (bigbuff, &b);
...
票数 0
EN

Stack Overflow用户

发布于 2012-04-23 13:54:46

这是最终的工作代码,目的是扫描目录中的文件,并将它们打印在带有修改日期的txt输出文件上:

代码语言:javascript
复制
#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;
}

感谢大家的帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10269132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档