gpt4 book ai didi

C Readdir 不读取所有文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:21 27 4
gpt4 key购买 nike

我正在尝试读取和处理目录中的所有文件。

如果只使用 readdir() 并计算目录中的文件数,一切都很好。但是,如果我复制文件名 strcpy() 并传递给不同的函数,readdir() 会在读取 4 个文件后返回 NULL

struct A {
char file_path[100];
double ave;
};

struct Calc {
struct A array[10];
char* max_filename;
double max;
char* min_filename;
double min;
};

int handle() {
DIR* fd;
int count = 0;
struct Calc *calc = calloc(sizeof(struct Calc), 0);

calc->min = -1;
calc->min_filename = NULL;
calc->max = 0;
calc->max_filename = NULL;

// open the directory and check
fd = opendir(DATA_DIR);
if (fd == NULL)
{
printf("Cannot open the directory %s\n", DATA_DIR);
closedir(fd);
return -1;
}

struct dirent * entry;
int file_count = 0;
while ((entry = readdir(fd)) != NULL) {
if (entry->d_type == DT_REG) { /* If the entry is a regular file */
file_count++;

if (count == 9)
{
D(calc);
count = 0;
}

struct A *a = &calc->array[count];
strcpy(a->file_path, entry->d_name);
calc->array[count].ave = 0;

C((void*)a);

count++;
}
}
printf("Total files: %d\n", file_count);

closedir(fd);
D(calc);

free(calc);
return 0;
}

int C(void *v)
{
// code
}

int D(void *v)
{
// code
}

谁知道这是什么原因?非常感谢!

最佳答案

calc = calloc(sizeof(struct Calc), 0); 那里的零看起来不对。 – 凯勒姆

关于C Readdir 不读取所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40962616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com