gpt4 book ai didi

linux - 在Linux中获取特定目录中的文件数

转载 作者:太空狗 更新时间:2023-10-29 11:23:27 25 4
gpt4 key购买 nike

有没有办法在不迭代 readdir(3) 的情况下获取特定目录中的文件总数?

我的意思是只有特定目录的直接成员。

似乎获得文件数量的唯一方法是重复调用 readdir(3) 直到它返回零。

有没有其他方法可以在 O(1) 中获取数字?我需要一个适用于 Linux 的解决方案。

谢谢。

最佳答案

scandir() 示例,需要 dirent.h:

struct dirent **namelist;
int n=scandir(".", &namelist, 0, alphasort); // "." == current directory.
if (n < 0)
{
perror("scandir");
exit(1);
}
printf("files found = %d\n", n);
free(namelist);

关于linux - 在Linux中获取特定目录中的文件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16930424/

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