c - stat() 返回错误

标签 c stat strftime localtime

我必须知道文件夹中某些文件的修改日期。它有效,但不适用于所有类型的文件。 例如,它适用于 .c、.txt,但不适用于其他类型,如 .mp4、.jpg 和 .mp3(我正在创建的应用程序通常必须处理多媒体文件)。它打印“无法显示时间。”,所以我想问题出在 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;
}

最佳答案

使用perror()。你不应该使用 st_mtime 吗?

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

99% sure it is because dir_p -> d_name does not exist, which in turn probably is because of a localization issue.

You could do something like:

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);
    }
} 

编辑2:

首先;我说getcwd() != NULL应该是==。 Selenium 变化。 (对我不好。)

您的代码中的问题。 (还有一些)但关于统计 - 您使用 readdir 中的 d_name。这只是文件名;不是目录+文件名。因此;你得到即:

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)) {

关于c - stat() 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10269132/

相关文章:

c - 运算符不被识别为加法。有办法吗?

c - 如何通过基本信号处理在c中同步多个进程

c++ - strftime - 将周数 ISO8601 转换为公历标准 C++

sql - 类似查询的不同结果取决于sqlite3中日期字符串的格式

c - netcdf c编程编译

c - 缓冲区溢出C

c - 缓存的未命中率是否有可能超过 100%

raku - perl6的stat函数

c - 从c中的文件中读取所有字节

具有国际感知日后缀的 PHP 日期格式?