title: C++ Random Access Files
body: <p>its my first time to experiment random access files in C++ however, I've tried it before in Java but I can't get it work in C++. The idea is I a want to create 100 empty records, and then store a record at a particular record number in the file. Here is my code, I kept it as simple as possible.</p>
  
  <p>I have here a structure named Tool:</p>
  
  <pre><code>struct Tool {
      int number;
      char name[20];
      int quantity;
      double cost;
  };
  </code></pre>
  
  <p>And here is what I have in the main function:</p>
  
  <pre><code>fstream outFile;
  outFile.open("inventory.dat");
  
  // Create 100 empty tool records
  Tool tool;
  tool.number = 0;
  tool.name[0] = '\0';
  tool.quantity = 0;
  tool.cost = 0;  
  
  for (int i = 0; i &lt; 100; i++) {
      outFile.write(reinterpret_cast&lt;char *&gt;(&amp;tool.number), sizeof(int));
      outFile.write(tool.name, sizeof(char)* 20);
      outFile.write(reinterpret_cast&lt;char *&gt;(&amp;tool.quantity), sizeof(int));
      outFile.write(reinterpret_cast&lt;char *&gt;(&amp;tool.cost), sizeof(double));
  }
  
  // Insert A tool record
  Tool t;
  t.number = 3;
  t.quantity = 7;
  t.cost = 57;
  strcpy(tool.name, "Electric Sander");
  
  outFile.seekp((tool.number - 1) * sizeof(Tool));
  outFile.write(reinterpret_cast&lt;char *&gt;(&amp;tool.number), sizeof(int));
  outFile.write(tool.name, sizeof(char)* 20);
  outFile.write(reinterpret_cast&lt;char *&gt;(&amp;tool.quantity), sizeof(int));
  outFile.write(reinterpret_cast&lt;char *&gt;(&amp;tool.cost), sizeof(double));
  
  outFile.close();
  </code></pre>
  
  <p>The part where I initialize 100 empty record works fine (assuming we are to comment to the insert section of the code.</p>
  
  <p>However, when the insert section is performed, my program generates 4 GB of data. I'm not sure what's going on. I appreciate any kind of help. Thanks in advance.</p>