title: Creating and Accessing Index Files in C++
body: <p>I'm supposed to read in a data file with fixed length records and fields, create a sorted index list in memory, and save that list to a file. Then I'm to write a second program that interactively (via the Linux command line) takes a key and the index file name, opens and loads the index file, searches for the given key using the index table, and opens and returns the correct data record.</p>
  
  <p>The original file consists of a list of records with a key (int), a name (string of 8 characters max), a code (int) and a cost (double).</p>
  
  <p>The RRN (relative record number) begins at 1, with the RRN at 0 representing a dummy record with only the size in the first entry.</p>
  
  <p>Here is the data file I will be using.</p>
  
  <pre><code>8 blank 0 0.0
  12345 Item06 45 14.2
  12434 Item04 21 17.3
  12382 Item09 62 41.37
  34186 Item25 18 17.75
  12165 Item16 30 7.69
  16541 Item12 21 9.99
  21212 Itme31 19 8.35
  41742 Item14 55 12.36
  </code></pre>
  
  <p>The execution is supposed to work in the following manner from the command line in Linux:</p>
  
  <pre><code>search 12382 prog5.idx
  </code></pre>
  
  <p>with prog5.idx being the index file created.</p>
  
  <p>I have some code written, but so far all it does is open the data file.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main() {
      ifstream data;
      data.open("prog5.dat");
      ofstream outFile("prog5.idx", ios::out);
  
      //if file can't be opened, exit
      if(!data) {
          cerr &lt;&lt; "Open Failure" &lt;&lt; endl;
          exit(1);
      }
      else {
          cout &lt;&lt; "File is open" &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>The "File is Open" part will be replaced once I figure out what do do once the file is open, just used this message to verify that it was opening the file. </p>
  
  <p>I've never worked with these kinds of files before so have no idea where to go from here. </p>