DIR *ptr;
struct dirent *ent, ent2;
int n=0, i=0;
dir = ".";
ptr = opendir(dir);
while((ent = readdir(ptr)) != NULL)
{
if(ent -> d_type != DT_DIR)
{
n++;
}
}
char array[n][100];
while((ent2 = readdir(ptr)) != NULL)
{
if(ent2 -> d_type != DT_DIR)
{
strcpy(array[i], ent2 -> d_name);
i++;
}
}
在上面的 C 代码中,我尝试计算工作目录中不包括文件夹的文件数。然后使用 count 声明一个双字符数组,然后用于存储文件名。我需要声明知道文件数量的双数组。(不允许声明大数组)。当我声明具有大尺寸的双精度数组并且只是将文件名复制到它而不进行任何计数时,该代码工作正常。据我了解,变量 ent 在第一个 while 循环中发生了变化。这就是我使用不同的“struct dirent ent2”的原因。