title: C++ Looping Through Files In Directory and Writing to a Different Directory
body: <p>I am attempting to modify some existing C++ code to work with my needs, but having never used C++ before, I am having some difficulties.</p>
  
  <p>My goal is:</p>
  
  <pre><code>--&gt; time and memory-intensive processes for preparation
  
  for each file in directory:
      open file;
      generate a tagged representation; //the current code just does this
      write file; //different directory but same filename
  </code></pre>
  
  <p>The reason I do not want to just call the C++ program for each file (with, for instance, a shell script) is that prior to the below code running, time and memory-intensive pre-processing steps are performed. (These take about 45-60sec. while the code only takes about 2-5sec. to run.)</p>
  
  <p>I have pasted the section of the code below. I want to read the arguments from the command line.</p>
  
  <pre><code>int main(int argc, char** argv) {
    /*
    pre-processing stuff
    */
  
    /* for each file */
    HANDLE hFind = INVALID_HANDLE_VALUE;
    string path = argv[1];
    string outpath = argv[2];
    WIN32_FIND_DATA ffd;
  
    //EDIT 2:
    cout &lt;&lt; "Path: " &lt;&lt; path &lt;&lt; '\n'; 
    cout &lt;&lt; "Outpath: " &lt;&lt; outpath &lt;&lt; '\n';
  
    hFind = FindFirstFile(path.c_str(), &amp;ffd);
    if (hFind == INVALID_HANDLE_VALUE) {
      cout &lt;&lt; "error searching directory\n";
      return false;
    }
  
    do {
      //istream *is(&amp;std::cin);
      string filePath = path + ffd.cFileName;
      ifstream in( filePath.c_str() );
      if (in) {
        /* for each line */
        string line;
        int n = 1;
        string str;
        string fullOutpath = outpath + ffd.cFileName;
        ofstream File;
        File.open(fullOutpath);
        while (getline(in, line)) {
          if (line.size() &gt; 1024) {
            cerr &lt;&lt; "warning: the sentence seems to be too long at line " &lt;&lt; n;
            cerr &lt;&lt; " (please note that the input should be one-sentence-per-line)." &lt;&lt; endl;
          }
  
          string postagged = bidir_postag(line, vme, vme_chunking, dont_tokenize);
  
          /* output to file */
          File &lt;&lt; postagged &lt;&lt; endl;
          //cout &lt;&lt; postagged &lt;&lt; endl;
  
          /* increment counter */
          n++;
        }
        File.close();
      } else {
        cout &lt;&lt; "Problem opening file " &lt;&lt; ffd.cFileName &lt;&lt; "\n";
      }
    } while (FindNextFile(hFind, &amp;ffd) != 0);
  
    if (GetLastError() != ERROR_NO_MORE_FILES) {
      cout &lt;&lt; "Something went wrong during searching\n"; 
    }
    return true;
  }
  </code></pre>
  
  <p>Currently, I am getting a compiler error: <strong>EDIT</strong>: compiler error fixed, thanks Blood!, but see below...</p>
  
  <pre><code>error: no matching function for call to 'std::basic_ofstream&lt;char&gt;::open&lt;std::string&amp;&gt;
  </code></pre>
  
  <p>Any thoughts? Please let me know if you need more code/information. Also, I should add that I'm running these on Windows XP using command prompt.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It now compiles (thanks Blood), though when it runs it is only attempting to open the directory, not the files in the directory.</p>
  
  <pre><code>Problem opening file directory_name.
  </code></pre>
  
  <p>The ifstream should be opening the files in teh directory, not the directory itself.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>I am running the executable fromt he command line with the following prompt:</p>
  
  <pre><code>.\tag.exe C:\indir C:\outdir
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>.\tag.exe C:\indir\* C:\outdir\
  </code></pre>
  
  <p>This enumerates all the files, but how can I capture them? Also, is there a simpler way to modify my code/input?</p>
  
  <p>I have also tried:</p>
  
  <pre><code>.\tag.exe C:\indir\ C:\outdir\
  </code></pre>
  
  <p>This gives: error searching directory.</p>
  
  <p><strong>EDIT 3:</strong></p>
  
  <p>Using:</p>
  
  <pre><code>.\tag.exe "C:\indir\*" C:\outdir\
  </code></pre>
  
  <p>I get the output:</p>
  
  <pre><code>Problem opening file .
  
  Problem opening file ..
  
  Problem opening file 2967
  
  Problem opening file 2966
  
  Problem opening file 4707
  
  etc. (100s)
  </code></pre>
  
  <p><strong>Solution:</strong></p>
  
  <p>Here are the key changes to the code (thanks Nate Kohl!):</p>
  
  <pre><code>string path = argv[1];
  path += "\\*";
  
  hFind = FindFirstFile(path.c_str(),&amp;ffd);
  
      // in the 'do-while' loop
      string filePath = argv[1];
      filePath += "\\";
      filePath += ffd.cFileName;
  
      ifstream in(filePath.c_str());
  
      //regarding the outpath
      fullOutpath = outpath + "\\";
      fullOutpath += ffd.cFileName;
      File.open(fullOutpath.c_str());
  </code></pre>
  
  <p>and from the command line:</p>
  
  <pre><code>.\tag.exe C:\indir C:\outdir
  </code></pre>
  
  <p>The help was very much appreciated.</p>