title: readdir looping more times than number of files present in the directory
body: <p>My goal is to count the number of files in a directory. After searching around, I found a piece of code which iterates over each file in a directory. But the issue is that it's looping extra times, 2 times extra to be more precise. </p>
  
  <p>So for </p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>My folder contains 500 files, but the output is shown till 502<img src="https://i.stack.imgur.com/zCVnP.jpg" alt="enter image description here"></p>
  
  <h3>UPDATE</h3>
  
  <p>I modified the code to read as</p>
  
  <pre><code>struct stat buf;
  if ( S_ISREG(buf.st_mode) ) // &lt;-- 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);
  }
  </code></pre>
  
  <p>But I'm getting <code>storage size of "buf" isn't known</code>. I also tried doing <code>struct stat buf[100]</code>, but that didn't help either.</p>