我编写了一个小函数,该函数接收文件夹的路径,并应返回其中的文件数。
由于某种原因,我的答案是不正确的:打印时,我看到它会打印.
和..
。
有人可以告诉我为什么吗?减少会2
解决吗?
这是代码:
{
struct dirent *entry;
DIR *dirp;
int fileCount = 0;
char currPath[MAX_STR];
dirp = opendir(clientLocalPath);
printf(" ***** printing files!!!!! ********\n");
while((entry = readdir(dirp)) != NULL)
{
strcpy(currPath, entry->d_name);
printf("%s\n",currPath);
fileCount++;
}
closedir(dirp);
return fileCount;
}
这是正常的,这是因为这两个.
和..
是目录的一部分:.
指当前和..
父。
每个目录都包含
.
和..
条目。
.
表示目录本身。它称为当前目录。
..
表示目录的父目录,即包含该目录的目录。
strcmp
如果您不想列出它们,最好的方法就是忽略它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句