C - 统计结构无法正常工作

C - stat struct not working properly

我正在尝试打印目录中文件的某些特征(仅限以小写字母开头的文件)。但是,当我执行以下代码时,其中一些有效,而另一些则无效。看起来 stat 结构可能无法正常工作,但到目前为止我还没有能够识别错误。

struct dirent *ep;
DIR *dp;    
char* cwd;
char buff[PATH_MAX + 1];
off_t tamTotal=0;
struct stat archivo;    
char path[]="/home/edu/Escritorio/P7/practica7/prueba";

if(!(dp=opendir(path))){
    printf("Error.\n");
    exit(-1);
}

printf("\nFILES:\n");
while (ep = readdir (dp)){ 
    stat(ep->d_name, &archivo);
    if(S_ISREG(archivo.st_mode)){
        if(!isupper(ep->d_name[0])){
            printf("  %s\n",ep->d_name);
            printf("    Last modification date: %s \n",ctime(&archivo.st_mtime));
            printf("    i-no number: %lu \n",archivo.st_ino);
            printf("    Blocks: %lu \n",archivo.st_blocks);
            printf("    Size: %lu \n",archivo.st_size);
        }       
    }
}   

输出:

很可能由 readdir 编辑的 dirent 结构 return 仅包含名称,而不包含目录或完整路径。

因此,stat 将失败,您没有注意到这一点,因为您没有检查它的 return 值。