gpt4 book ai didi

c - readdir 循环次数超过目录中存在的文件数

转载 作者:太空宇宙 更新时间:2023-11-04 03:57:34 25 4
gpt4 key购买 nike

<分区>

我的目标是计算目录中的文件数。四处搜索后,我发现了一段代码,它遍历目录中的每个文件。但问题是它循环了额外的时间,为了更精确,额外循环了 2 次。

所以对于

int main(void)
{
DIR *d;
struct dirent *dir;
char *ary[10000];
char fullpath[256];
d = opendir("D:\\frames\\");
if (d)
{
int count = 1;
while ((dir = readdir(d)) != NULL)
{
snprintf(fullpath, sizeof(fullpath), "%s%d%s", "D:\\frames\\", count, ".jpg");
int fs = fsize(fullpath);
printf("%s\t%d\n", fullpath, fs); // using this line just for output purposes
count++;
}
closedir(d);
}
getchar();
return(0);
}

我的文件夹包含 500 个文件,但输出显示到 502 enter image description here

更新

我将代码修改为

struct stat buf;
if ( S_ISREG(buf.st_mode) ) // <-- I'm assuming this says "if it is a file"
{
snprintf(fullpath, sizeof(fullpath), "%s%d%s", "D:\\frames\\", count, ".jpg");
int fs = fsize(fullpath);
printf("%s\t%d\n", fullpath, fs);
}

但我得到“buf”的存储大小未知。我也尝试过执行 struct stat buf[100],但这也没有帮助。

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