为什么与stat READDIR后不工作? [英] Why is stat not working after readdir?

查看:221
本文介绍了为什么与stat READDIR后不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了打印目录名或文件名的程序。这很容易,但我得到的东西的麻烦。
它不能区分目录和文件类型。我知道,我用stat.st_mode完成它。但蹊跷的:

当我使用gdb来检查ST_MODE价值,我发现它是0,除了。和..,所以这里是一个问题:为什么ST_MODE为0

这就是我的code:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&dirent.h GT;
#包括LT&; SYS / stat.h>INT主要(无效)
{
DIR * PDIR =执行opendir(MyDirectory);
结构的dirent * pDirent;
结构统计VSTAT;如果(PDIR == NULL)
{
    的printf(无法打开目录\\MyDirectory \\);
    出口(1);
}而((pDirent = READDIR(PDIR))!= NULL)
{
    STAT(pDirent-> d_name,&安培; VSTAT);
    如果(S_ISDIR(vStat.st_mode))
        的printf(目录:%S \\ n,pDirent-> d_name);
    其他
        的printf(文件:%s \\ n,pDirent-> d_name);
}closedir(PDIR);
返回0;
}


解决方案

经典 READDIR 错误: pDirent-> d_name 是目录条目,而不是文件的路径名。这是14-5.c等,所以你的统计呼叫正在寻找该名称的在当前目录的,而不是在 MyDirectory

文件

检查返回值统计。你会看到它的 ENOENT - 除了 .. ,它存在于当前目录中。当统计失败,stat结构的内容是不确定的。

如果你比其他的目录中调用执行opendir ,然后做pretty多任何与返回的名字是有用的,你需要建立一个完整路径。复制你传递给执行opendir 与足够的空间,另外一个斜杠和文件名的缓冲路径,每个文件名复制到缓冲区。验证的概念,code(检查省略错误等):

 的char *目录=MyDirectory;
为size_t directory_length = strlen的(目录);
字符*路径=的malloc(directory_length + 1 + NAME_MAX);
的strcpy(路径,目录);
路径[directory_length] ='/';
而((pDirent = READDIR(PDIR))!= NULL){
    的strcpy(路径+ directory_length + 1,pDirent-> d_name);
    如果(STAT(路径和放大器; VSTAT)== -1){
        PERROR(路径);
        继续;
    }
    ...
}

I wrote a program that print the directory name or file name. It's easy but I got something trouble. It couldn't distinguish directory and file type. I know and I used stat.st_mode to finish it. But something wrong:

when I use gdb to check the st_mode value, I found it was 0, except "." and "..", so here is the question: why st_mode is 0?

and that is my code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>

int main(void)
{
DIR *pDir = opendir("MyDirectory");
struct dirent *pDirent;
struct stat vStat;

if (pDir == NULL)
{
    printf("Can't open the directory \"MyDirectory\"");
    exit(1);
}

while ((pDirent = readdir(pDir)) != NULL)
{
    stat(pDirent->d_name, &vStat);
    if (S_ISDIR(vStat.st_mode))
        printf("Directory: %s\n", pDirent->d_name);
    else
        printf("File: %s\n", pDirent->d_name);
}

closedir(pDir);
return 0;
}

解决方案

Classic readdir mistake: pDirent->d_name is the name of the directory entry, not a path to the file. It's "1", "4-5.c", etc. So your stat calls are looking for a file with that name in the current directory, not under MyDirectory.

Check the return value of stat. You'll see that it's ENOENT — except for . and .., which exist in the current directory as well. When stat fails, the content of the stat structure is undefined.

If you're calling opendir in a directory other than ., then to do pretty much anything useful with the returned names, you need to build a full path. Copy the path you passed to opendir to a buffer with enough room for a slash and file name in addition, and copy each file name to that buffer. Proof-of-concept code (error checking omitted, etc.):

char *directory = "MyDirectory";
size_t directory_length = strlen(directory);
char *path = malloc(directory_length + 1 + NAME_MAX);
strcpy(path, directory);
path[directory_length] = '/';
while ((pDirent = readdir(pDir)) != NULL) {
    strcpy(path + directory_length + 1, pDirent->d_name);
    if (stat(path, &vStat) == -1) {
        perror(path);
        continue;
    }
    …
}

这篇关于为什么与stat READDIR后不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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