title: How to read all files in a folder using C
body: <p>I wish to read all the text files in a particular folder. The files' names do not have any common pattern in them- else the task would have been easier.</p>
  
  <pre><code>//read a file from the directory  
  //Perform a common operation  
  //write output to a common file  
  //read the next file
  </code></pre>
  
  <p>It will be good if I could work around with sub-folders as well, but even the basic implementation is sufficient.</p>
  
  <p>I tried looking at the previously asked related questions (<a href="https://stackoverflow.com/q/2918579/1295278">here</a>, <a href="https://stackoverflow.com/q/1844688/1295278">here</a>, <a href="https://stackoverflow.com/q/5840443/1295278">here</a> and <a href="https://stackoverflow.com/q/5426398/1295278">here</a>), but none of them give a C and Linux specific answer which I need. </p>
  
  <p><strong>edit</strong> : So, this is what I wrote based on the answers received-</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;errno.h&gt;
  
  
  int main(int argc, char **argv)
  {
      DIR* FD;
      struct dirent* in_file;
      FILE    *output_file;
      FILE    *entry_file;
      char    buffer[BUFSIZ];
  
      /* Opening common file for writing */
      output_file = fopen("/home/pnp/snort_rules_folder/rulesoutput.txt", "a+");
      if (output_file == NULL)
      {
          fprintf(stderr, "Error : Failed to open output_file\n");
  
          return 1;
      }
  
      /* Scanning the in directory */
      if (NULL == (FD = opendir ("/home/pnp/snort_rules_folder/rules"))) 
      {
          fprintf(stderr, "Error : Failed to open input directory\n");
          fclose(output_file);
  
          return 1;
      }
      while ((in_file = readdir(FD))) 
      {
          /* On linux/Unix we don't want current and parent directories
           * If you're on Windows machine remove this two lines
           */
          if (!strcmp (in_file-&gt;d_name, "."))
              continue;
          if (!strcmp (in_file-&gt;d_name, ".."))    
              continue;
          /* Open directory entry file for common operation */
          /* TODO : change permissions to meet your need! */
          entry_file = fopen(in_file-&gt;d_name, "r");
          if (entry_file == NULL)
          {
              fprintf(stderr, "Error : Failed to open entry file\n");
              fclose(output_file);
  
              return 1;
          }
  
          /* Doing some stuff with entry_file : */
  
          while (fgets(buffer, BUFSIZ, entry_file) != NULL)
          {
              /* Use fprintf or fwrite to write some stuff into common_file*/
          }
  
      fprintf(output_file, "reading file %s", in_file-&gt;d_name);
  
          /* When you finish with the file, close it */
          fclose(entry_file);
      }
  
      /* Don't forget to close common file before leaving */
      fclose(output_file);
  
      return 0;
       }
  </code></pre>
  
  <p>And the error received-<br>
       pnp@pnp-laptop:~/snort_rules_folder$ ./a.out<br>
       Error : Failed to open entry file</p>