Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C readdir循环次数超过目录中存在的文件数_C_Readdir - Fatal编程技术网

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

C readdir循环次数超过目录中存在的文件数,c,readdir,C,Readdir,我的目标是计算目录中的文件数。在四处搜索之后,我发现了一段代码,它迭代了目录中的每个文件。但问题是它的循环次数增加了,更准确地说是增加了2倍 所以 int main(void) { DIR *d; struct dirent *dir; char *ary[10000]; char fullpath[256]; d = opendir("D:\\frames\\"); if (d) { int count = 1;

我的目标是计算目录中的文件数。在四处搜索之后,我发现了一段代码,它迭代了目录中的每个文件。但问题是它的循环次数增加了,更准确地说是增加了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

更新 我将代码修改为

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);
}
struct stat buf;

如果(S_ISREG(buf.st_mode))/如评论中所述,您还将获得名为
的两个目录,这会扭曲您的计数

在Linux中,您可以使用
struct dirent
d_type
字段来过滤它们,但是文档中说:

POSIX.1规定的dirent结构中的唯一字段是:
d_name[]
,未指定大小,在终止的空字节之前最多包含
name_MAX
个字符;和(作为XSI扩展)
d_ino
。其他字段未标准化,并非所有系统都存在;有关更多详细信息,请参见下面的注释


因此,假设您在Windows上,您可能没有
d\u type
。然后您可以使用其他调用来代替,例如。当然,您也可以根据名称进行筛选,但如果您想跳过目录,这是一个更为健壮和通用的解决方案。

正如在评论中指出的,您还可以得到名为
的两个目录。
,这会使您的计数发生偏差

在Linux中,您可以使用
struct dirent
d_type
字段来过滤它们,但是文档中说:

POSIX.1规定的dirent结构中的唯一字段是:
d_name[]
,未指定大小,在终止的空字节之前最多包含
name_MAX
个字符;和(作为XSI扩展)
d_ino
。其他字段未标准化,并非所有系统都存在;有关更多详细信息,请参见下面的注释


因此,假设您在Windows上,您可能没有
d\u type
。然后您可以使用其他调用来代替,例如。当然,您也可以根据名称进行筛选,但如果您想跳过目录,这是一个更为健壮和通用的解决方案。

您需要调用
\u stat()
/
stat()

#include <sys/types.h>
#include <sys/stat.h>

#ifdef WINDOWS
#  define STAT _stat
#else
#  define STAT stat
#endif

...

char * filename = ... /* let it point to some file's name */

struct STAT buffer = {0};
if (STAT(filename, &buffer)
  ... /* error */
else
{
  if (S_ISREG(buffer.st_mode))
  {
    ... /* getting here, means `filename` referrs to a ordinary file */
  }
}
#包括
#包括
#ifdef窗口
#定义STAT\u STAT
#否则
#定义统计数据
#恩迪夫
...
char*文件名=…/*让它指向某个文件名*/
struct STAT buffer={0};
if(STAT(文件名和缓冲区)
…/*错误*/
其他的
{
if(S_ISREG(缓冲区st_模式))
{
…/*到达这里,意味着“filename”指的是一个普通文件*/
}
}

您需要调用
\u stat()

#include <sys/types.h>
#include <sys/stat.h>

#ifdef WINDOWS
#  define STAT _stat
#else
#  define STAT stat
#endif

...

char * filename = ... /* let it point to some file's name */

struct STAT buffer = {0};
if (STAT(filename, &buffer)
  ... /* error */
else
{
  if (S_ISREG(buffer.st_mode))
  {
    ... /* getting here, means `filename` referrs to a ordinary file */
  }
}
#包括
#包括
#ifdef窗口
#定义STAT\u STAT
#否则
#定义统计数据
#恩迪夫
...
char*filename=…/*让它指向某个文件的名称*/
struct STAT buffer={0};
if(STAT(文件名和缓冲区)
…/*错误*/
其他的
{
if(S_ISREG(缓冲区st_模式))
{
…/*到达这里,意味着“filename”指的是一个普通文件*/
}
}

这是
目录。您应该检查
dirent
结构的
d\u type
成员,以确保您有一个常规文件。您不会丢弃目录,每个文件夹中都隐含着两个目录:“.”和“.”。应该测试并忽略这些。因为您关心的只是文件,所以我建议您测试对象是否为常规文件,如果不是,则忽略它(这将解决此问题和另一个您还不知道的问题)。是的。这是正确的。当我尝试使用对象打印文件名时,我得到了
的两个额外迭代。
。让我看看如何告诉脚本忽略它们。使用
dir->d_type
似乎不起作用。我得到的
结构没有名为d_type的成员n page这样说:未签名的char d_type;/*类型的文件;并非所有文件系统类型都支持*/这是
目录。
目录。您应该检查
dirent
结构的
d_type
成员,以确保您有一个常规文件。您没有抛出目录,其中有两个目录每个文件夹中都有:“.”和“.”。应该测试并忽略这些文件。因为您关心的只是文件,所以我建议您测试对象是否为常规文件,如果不是,则忽略它(这将解决此问题和另一个您还不知道的问题)。是的。这是正确的。当我尝试使用对象打印文件名时,我得到了
的两个额外迭代。
。让我看看如何告诉脚本忽略它们。使用
dir->d_type
似乎不起作用。我得到的
结构没有名为d_type的成员n page这样说:未签名的char d_type;/*文件类型;不受所有文件系统类型的支持*/我确实在windows上。我确实找到了与
stat()
相关的内容。请检查我的更新答案(发布此评论后将进行更改)看看有什么问题吗?我确实在windows上。我确实找到了一些与
stat()
相关的东西。你能检查一下我的更新答案(在发布此评论后将进行更改)并看看有什么问题吗?你能用我提供的代码来描述这个问题吗?你能用我提供的代码来描述这个问题吗?