简体   繁体   English

使用readdir_r读取目录

[英]Reading directories using readdir_r

I've been checking and fighting with this procedure without success for quite a while, hope you can help me out. 很长一段时间以来,我一直在检查和使用这个程序而没有成功,希望你能帮助我。

The idea is to read the directory stored in c_Localpath and copy the read dirs into c_namesLocal to return them. 想法是读取存储在c_Localpath的目录,并将读取的目录复制到c_namesLocal以返回它们。

Am I doing something wrong in my implementation? 我在实施中做错了吗? The program breaks on the strcpy , and I have no clue why. 该程序打破了strcpy ,我不知道为什么。

DIR* ptr_dir = opendir(c_Localpath);

char** c_namesLocal = calloc(1, 256);

    size_t numElements = 0;
    int returnCode =0;
    struct dirent ptr_PrevDirEntry;
    struct dirent* ptr_DirEntry = NULL;
    returnCode = readdir_r(ptr_dir, &ptr_PrevDirEntry,
            &ptr_DirEntry);

    while ((returnCode ==0) && (ptr_DirEntry != NULL)) {
        char* name = c_namesLocal[numElements];
        strcpy(name, ptr_DirEntry->d_name);
        ptr_PrevDirEntry = *ptr_DirEntry;
        returnCode = readdir_r(ptr_dir, &ptr_PrevDirEntry,
                &ptr_DirEntry);
        numElements++;
        c_namesLocal = realloc(c_namesLocal, 256 * numElements);
    }

Sorry for replying after 2 years. 很抱歉2年后回复。 But I wanted to help others who would like to know the answer. 但我想帮助那些想知道答案的人。 I have worked on the implementation of readdir_r and thus I have modified your code to make it work. 我已经致力于readdir_r的实现,因此我修改了你的代码以使其工作。 I cannot say the codes as a whole would work but, it is definite that you could get the next file entry in the indicated directory you mentioned. 我不能说代码作为一个整体会起作用,但是,您可以确定您可以在您提到的指定目录中获取下一个文件条目。 And thus you could save those entries into other var and could do whatever you would like to. 因此,您可以将这些条目保存到其他var中,并可以执行您想要的任何操作。

Please see below. 请看下面。

DIR* ptr_dir = opendir(c_Localpath);
size_t numElements = 0;
int returnCode =0;
struct dirent *ptr_PrevDirEntry = NULL;
struct dirent *ptr_DirEntry = NULL;
int len_entry;
char *c_namesLocal = NULL;

len_entry = offsetof(struct dirent, d_name) + fpathconf(dirfd(ptr_dir), _PC_NAME_MAX) + 1;
ptr_PrevDirEntry = malloc(len_entry);

if(!ptr_PrevDirEntry)
        exit(0);

for(;;){
        readdir_r(ptr_dir, ptr_PrevDirEntry, &ptr_DirEntry);
        if(!ptr_DirEntry)
            break;
        else
        {
                 if((strcmp(ptr_DirEntry->d_name, ".") != 0) && (strcmp(ptr_DirEntry->d_name, "..") != 0)) // skip "." and ".." file listings
                 {
                    //Perform copying or do whatever you want with the file entry read from the dir "c_Localpath"
                    //Increase numElements to 1 and keep on increasing by 1 on every iteration
                    numElements++;
                    //realloc everytime you find next entry
                    c_namesLocal = realloc(c_namesLocal, 256 * numElements);
                    //copy the next file name in the c_namesLocal[0], c_namesLocal[1] and so on.
                    strcpy(c_namesLocal[numElements - 1], ptr_DirEntry->d_name);
                 }
        }
 }

 //free "ptr_PrevDirEntry" before returning and take care of "c_namesLocal" as well.

The codes above are self-explanatory. 上面的代码是不言自明的。 I hope it helps you. 我希望它对你有所帮助。 Cheers :) 干杯:)

This is a null pointer exception, c_namesLocal actually points to a sequence of 256/sizeof(char*) char * pointers all set to null by calloc, so c_namesLocal[numElements] == 0 . 这是一个空指针异常, c_namesLocal实际上指向一个由calloc设置为null的256 / sizeof(char *)char *指针的序列,因此c_namesLocal[numElements] == 0 You have to allocate memory for each entry. 您必须为每个条目分配内存。

In this code: 在这段代码中:

char* name = c_namesLocal[numElements];
strcpy(name, ptr_DirEntry->d_name);

name starts as NULL. name以NULL开头。 You initialized a vector of pointers, but the single pointers themselves still point to NULL. 你初始化了一个指针向量 ,但是单个指针本身仍然指向NULL。 You ought to allocate memory for the d_name : 你应该为d_name分配内存:

// You no longer need '*name'
c_namesLocal[numElements] = strdup(ptr_DirEntry->d_name);

Then it would be prudent to add a check: 那么添加支票是明智的:

if (NULL == (c_namesLocal[numElements] = strdup(ptr_DirEntry->d_name)))
{
    // signal out of memory and return
}

And the same check when you realloc the vector of pointers. 当你重新分配指针向量时,同样检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM