title: C++ read new files added to a directory
body: <p>I have a C++ program that when starts, gets the list of all files in a directory and does some processing with them (packetizes them for transmission over network). The current version is static and the program only sees the files at the time the program starts. Now I want to change it to a dynamic version where it can continuously see the new files added in the directory (let's assume they have a meaningful incremented name; for instance f1, f2, f3, ... so next one coming would be f4. This way I can use some index).</p>
  
  <p>Right now this is the piece of code (static reading of all files):</p>
  
  <pre><code>DIR *dp;
  struct dirent *dirp;
  char * dir = new char[inData-&gt;dir.length() + 1];
  strcpy(dir, inData-&gt;dir.c_str());
  dp = opendir(dir);
  int file_index = 0;
  int total_files = DataUtil::count_files(dp);
  
  if (readdir(dp) == NULL)
      rewinddir(dp);
  
  //for each file in the directory
      while ((dirp = readdir( dp ))) {
          string f_name = dirp-&gt;d_name;
          if (f_name != "." &amp;&amp; f_name != "..") {  // except . and ..
              //Do some processing
              my_function(f_name, file_index);
              file_index++;               
          }
      }
      closedir(dp);
  </code></pre>
  
  <p>I modified the code using the <code>rewinddir(dp);</code> function and <code>file_index</code> to notice new files. But it doesn't work. Where is the problem?</p>
  
  <pre><code>DIR *dp;
  struct dirent *dirp;
  char * dir = new char[inData-&gt;dir.length() + 1];
  strcpy(dir, inData-&gt;dir.c_str());
  dp = opendir(dir);
  int file_index = 0;
  int total_files = MyDataUtil::count_files(dp);
  
  if (readdir(dp) == NULL)
      rewinddir(dp);
  
  //for each file in the directory
      while ((dirp = readdir( dp ))) {
          string f_name = dirp-&gt;d_name;
          if (f_name != "." &amp;&amp; f_name != "..") {
          //skip files already read
          for (int var = 0; var &lt; file_index; var++) {
              readdir(dp);
          }
              //Do some processing
              my_function(f_name, file_index);
              file_index++;
  
          //reset dir stream, and update total_files
          rewinddir(dp);
          total_files = MyDataUtil::count_files(dp);            
          }
      }
      closedir(dp);
  </code></pre>
  
  <p>The total_files function:</p>
  
  <pre><code>int MyDataUtil::count_files(DIR *dp){
      struct dirent *dirp;
      int num = 0;
  
      while ((dirp = readdir( dp ))) {
          string f_name = dirp-&gt;d_name;
          if (f_name != "." &amp;&amp; f_name != "..")
              num++;
      }
      return num;
  }
  </code></pre>
  
  <p>Here is the <code>rewinddir</code> documentation. It says ' might or might not be returned'!</p>
  
  <pre><code>Description:
  
  The rewinddir function is used to reinitialize the directory stream dirstream, so that if you call readdir 
   it returns information about the first entry in the directory again. This function also notices if files 
   have been added or removed to the directory since it was opened with opendir. (Entries for these files 
   might or might not be returned by readdir if they were added or removed since you last called opendir 
   or rewinddir.) 
  </code></pre>