STAT()返回错误 [英] stat() returns error

查看:147
本文介绍了STAT()返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须知道一些文件夹中的文件的修改日期。它的工作原理,但并不适用于所有类型的文件。
例如,它可以与.C,txt文件,但它不与其他类型,比如.MP4,jpg和.MP3(我创建的应用程序具有一般与多媒体文件工作)工作。它打印出无法显示的时间。,所以我想这个问题是统计上的()。谢谢你。

这是code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&dirent.h GT;
#包括LT&;&string.h中GT;
#包括LT&;&time.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / stat.h>炭帕罗拉[12] =,哈希[32] =,esadecimale [1000] =,系统3 [100] =./ MD5;
INT I,LEN,LEN2;
诠释字节;
焦炭CWD [1024];INT主要(无效)
{
字符T [100] =;
struct stat中B:
DIR * DP;
焦炭destinationFolder [100] =/用户/ mattiazeni /桌面/ PROVA /; //我啦帕萨特达迪索普拉
结构的dirent * DIR_P;
DP =执行opendir(destinationFolder);
如果(DP == NULL)出口(1);LEN = strlen的(destinationFolder);对于(i = 0; I< LEN,我++){
    系统3 [1 + 6] = destinationFolder [I];
}而((DIR_P = READDIR(DP))!= NULL){
    如果(DIR_P - >!'。'd_name [0] =){
        //的printf(%S \\ n,DIR_P - > d_name);
        LEN2 = strlen的(DIR_P - > d_name);
        对于(i = 0; I< LEN2;我++){
            如果(DIR_P - > d_name [I] ==''){//弥服务每correggere我能美代文件CON spazi
                系统3 [I + LEN + 6] ='\\\\';
            }
            否则系统3 [I + LEN + 6 = DIR_P - > d_name [I];
        }
        系统(系统3); //帕萨特IL valore一个MD5澈calcola L'哈希Ë罗斯坦帕道文件澈CI服务Insieme酒店人persorso /诺姆德尔文件        FILE * FP;
        如果((FP = FOPEN(userDatabase.txt,AB))== NULL){
            的printf(错误打开文件... \\ n);
            FCLOSE(FP);
        }
        其他{
            如果(STAT(DIR_P - >!d_name,和b)){
            的strftime(T,100,%D /%M /%Y%H:%M:%S,本地时间(安培; b.st_mtime)); //C'è安科拉qualche errore!
            fprintf中(FP,%S,T);
            }
            其他{
                PERROR(0);
                fprintf中(FP,错误);
            }
            fprintf中(FP初始化);
            fprintf中(FP,\\ n);
        }
        FCLOSE(FP);
        对于(i = LEN + 6; I< LEN + 6 + LEN2;我++){
            系统3 [I] ='';
        }
    }
}
closedir(DP);
返回0;
}


解决方案

使用 PERROR()。也不宜使用 st_mtime

 统计:
       如果成功,返回零。
       在错误时,返回-1,和错误号设置为合适。

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

您可以这样做:

  fprintf中(标准错误,
        无法与stat%S \\ n,
        dir_p-> d_name);
PERROR(0);

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

和您的 FCLOSE(FP) FP == NULL 检查之外。当你不回或中止流量你可能一个SIGSEGV如果的fopen 失败。


编辑:你是什么像这样

 的#include<&unistd.h中GT;焦炭CWD [1024];...
}其他{
    fprintf中(标准错误,
            无法与stat'%s'的\\ n
            dir_p-> d_name);
    PERROR(0);    如果(GETCWD(CWD,sizeof的(CWD))== NULL){
        PERROR(GETCWD()错误);
    }其他{
        fprintf中(标准错误,
                ,在目录%s'\\ n
                CWD);
    }
}


EDIT2:

第一;我说 GETCWD()!= NULL == 。硒的变化。 (由我不好。)

在code中的问题。 (有几个),但有关统计 - 您使用从 READDIR d_name。这是的只有的文件名;不DIR +文件名。从而;你即:

  STAT(dir_p-> d_name,...)

这已经成为即:

  STAT(file.mp4,...)

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

  / *您需要您之后终止系统串for循环* /
系统3 [I + LEN + 6] ='\\ 0';系统(系统3);如果(!STAT(系统3 + 6,和b)){

I have to know the modification date of some files in a folder. It works, but not with all types of files. For example it works with .c, .txt, but it doesn't work with other types such .mp4, .jpg and .mp3 (the application I'm creating have to work with multimedia files in general). It prints "Cannot display the time.", so I suppose the problem is on stat(). Thanks.

This is the code:

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

解决方案

Use perror(). Also shouldn't you use 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);

Also; shouldn't it be ->f_name and not ->d_name if you are checking file status? - (Unless you use d_name for file name off course.)

And your fclose(fp) is outside your fp == NULL check. As you do not return or otherwise abort the flow you risk an SIGSEGV if the fopen fail.


Edit: What do you get with something like this?

#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:

First; I said getcwd() != NULL should be ==. Se change. (Bad by me.)

The problem in your code. (There is a few more) but regarding stat - you use d_name from readdir. This is only filename; not dir+filename. Thus; you get i.e.:

stat(dir_p->d_name, ...)

Which becomes i.e.:

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

Easiest quick-fix (tho dirty) would be:

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

system(system3);

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

这篇关于STAT()返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆