title: Writing a C++ program that can merge files from the command line in Linux
body: <p>I have written a C++ program that is supposed to open two text files (<strong>prog2a.dat</strong> and <strong>prog2b.dat</strong>) and write the contents of a specified range of lines to an output file (<strong>outfile.dat</strong>). I wrote a program that based on the example we were given (to take lines <code>5-15</code> from the first file and lines <code>4-12</code> of the second file and merge them into the output file) works perfectly. However, after asking my professor for some clarification on another part of the assignment, I found out I have not done this correctly. I have the code written so that it will always output the range of lines I mentioned earlier, but the program is actually supposed to allow the user to merge the files from the command line using whatever range they want by typing in the following command:</p>
  
  <pre><code>prog2 in1 5-15 in2 4-12 outfile
  </code></pre>
  
  <p>But I'm not sure how to tweak my current program to allow this to be done. </p>
  
  <p>Here is the code that I have written, keep in mind that this works properly for the way it is written, but not how it is supposed to work for the purposes of the command line (hopefully that makes sense):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  int main() {
      // Create output file
      std::ofstream outFile("outfile.dat", ios::out);
  
      // Open input file 1, if can't be opened, exit
      ifstream in1;
      in1.open("prog2a.dat");
      std::string line;
      int count = 1;
      if (!in1) {
          cerr &lt;&lt; "Open Failure" &lt;&lt; endl;
          exit(1);
      } // end if
      else {
          while (std::getline(in1, line)) {
              if (count &gt;= 5 &amp;&amp; count &lt;= 15) {
                  outFile &lt;&lt; line &lt;&lt; "\n"; /*writes the contents of
                  lines 5-15 to outfile.dat*/
              }
              ++count;
          } // end while
      } // end else
      in1.close(); // close in1 (prog2a.dat)
      outFile &lt;&lt; "\n"; // add a blank line after the output from prog2a.dat
      count = 1; // reset the line count to 1 before opening next file.
  
      // Open input file 2, if can't be opened, exit
      ifstream in2;
      in2.open("prog2b.dat");
      if (!in2) {
          cerr &lt;&lt; "Open Failure" &lt;&lt; endl;
          exit(1);
      } // end if
      else {
          while (std::getline(in2, line)) {
              if (count &gt;= 4 &amp;&amp; count &lt;= 12) {
                  outFile &lt;&lt; line &lt;&lt; "\n"; /*writes the contents of the
                  lines 4-12 to outfile*/
              }
              ++count;
          } // end while
      } // end else
      in2.close(); // close in2 (prog2b.dat)
  } // end main
  </code></pre>
  
  <p>Is there any simple way to make this work as I described using the command line? Also, I am supposed to break this up into three files, a header file, the program file, and a test file (the test file contains <code>main()</code> and should close the 3 open files and display any error messages), but I'm getting really confused as to what should go in the header file. I know the header file should contain class definitions and constructors, but don't really know how to make that work for this specific program? I'm extremely new to this so any pointers would be greatly appreciated. </p>