title: How to extract a directory recursively?
body: <p>I'm trying to extract a directory recursively using the function below. When I run the program it is able to create a root directory and the files inside of it but it is not able to completely write the files inside of the sub directories. </p>
  
  <p>My code is able to compile but I keep getting a segmentation fault error. I have tried looking for rogue pointers in my function or memory that should not be other wised accessed but I am getting nowhere. Any help or guidance in the right direction would be greatly appreciated! </p>
  
  <p>Here is my Extraction function:</p>
  
  <pre><code>int DExtract(FILE* fp, char* outputName, char* inputName, DIR* d, bool root)
  {
        DIR* dsub;
      struct dirent* p;
      FILE* outF;
      int i=0;
      char fileName[BUFFER_SIZE];
      char SfileName[BUFFER_SIZE];
      bool SC;
      int dz;
      int nameLength;
      int fileSize;
      int fileType;
  
  ////// ROOT CREATION /////////////
  if(root)
  {
         fscanf(fp,"%d %d %s %d\n", &amp;fileType, &amp;nameLength, inputName, &amp;fileSize);
     if(fileType==0)
     {
     mkdir(outputName, 0755); // create root directory
     }
  
  }
  //////////////////////////////////
  
  root = false;  // MOVING INTO ROOT
  d = opendir(outputName);   // open directory with input name
  
  while( (p = readdir(d)) || (dz  != EOF))
     { fscanf(fp,"%d %d %s %s %d ",&amp;fileType, &amp;nameLength, p-&gt;d_name, fileName, &amp;fileSize);
  
  //////////////////////////////////////////////////
  
       strcpy(SfileName,outputName);
  
       for(int h=0; h &lt; strlen(fileName); h++ )
       {
          if( fileName[h] == '/')
          { SC = true;}
  
          if(SC == true)
          { strcat(SfileName, &amp;fileName[h]);
  
             h=strlen(fileName);
          }
       }
        SC = false;
       cout &lt;&lt; "STRING LENGTH FILENAME: " &lt;&lt; strlen(fileName) &lt;&lt; endl;
       cout &lt;&lt; "SFILENAME in WHILE: " &lt;&lt; SfileName &lt;&lt; endl;
  
  
  
  
  
  
   /////////////////////////////////////  /////////////////
  
      if(strcmp(p-&gt;d_name, ".") != 0) // if they are not the same as current working directory
      { if(strcmp(p-&gt;d_name, "..") != 0) // if they are not the same as parent directory
  
         { if(p-&gt;d_name[nameLength - 1] != '~')  // If not the same as home directory
              {
  
  ///////////////////////////
      if(fileType == 1)
      {
  
    // This is a regular file
  
       outF = fopen(SfileName, "w+");  // create file
       i=0;
  
  
       do
       {
         int ch;
         ch = fgetc(fp);
         dz = ch;
         if(i != (fileSize-1))
          {
            fputc(ch,outF);
            i++;
  
          }
  
       }while( i != (fileSize-1) );
       fclose(outF);
  
   }
   else if( fileType == 0)
   {
  
      // This is a directory
  
  cout &lt;&lt; "SFILENAME in DIRECTORY: " &lt;&lt; SfileName &lt;&lt; endl;
  
       mkdir(SfileName, 0755);   // create directory
  
       if((strcmp(SfileName, ".") != 0) )
       {
         dsub = opendir(SfileName);
         DExtract(fp, outputName, inputName, dsub, root);
  
           }
       }
      }
     }
    }
   }
  
  
    return 0;
  }
  </code></pre>