C - stat 结构无法正常工作

标签 c file struct stat

我正在尝试打印目录内文件的一些特征(仅限以小写字母开头的文件)。但是,当我执行以下代码时,其中一些可以工作,有些则不能。看起来 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);
        }       
    }
}   

输出:

enter image description here

最佳答案

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

因此,stat 将失败,您不会注意到这一点,因为您没有检查其返回值。

关于C - stat 结构无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283054/

相关文章:

c - SDL2 tilemap - 太慢

java - 文件扫描仪阵列

c - 如何链接多个 .c 文件

javascript - 对用户选择的 HTML 文件使用 DOM 方法

c - C语言链表结构中安全释放值的策略

objective-c - 将 Cocoa header 链接到 ruby​​ C 扩展

c - 在结构体中使用函数指针 - C 编译器

c - 如何用 char* 复制一个结构并指向它?

C - 结构和函数的前向声明

c - 如何从全屏 OpenGL 窗口获取关键事件?