title: Write and read records to .dat file C++
body: <p>I am quite new to C++ and am trying to work out how to write a record in the format of this structure below to a text file:</p>
  
  <pre><code>struct user {
      int id;
      char username [20];
      char password [20];
      char name [20];
      char email [30];
      int telephone;
      char address [70];
      int level;
  }; 
  </code></pre>
  
  <p>So far, I'm able to write to it fine but without an incremented id number as I don't know how to work out the number of records so the file looks something like this after I've written the data to the file.</p>
  
  <pre><code>1 Nick pass Nick email tele address 1
  1 user pass name email tele address 1
  1 test test test test test test 1
  1 user pass Nick email tele addy 1
  1 nbao pass Nick email tele 207 1
  </code></pre>
  
  <p>Using the following code:</p>
  
  <pre><code>ofstream outFile;
  
  outFile.open("users.dat", ios::app);
  
  // User input of data here
  
  outFile &lt;&lt; "\n" &lt;&lt; 1 &lt;&lt; " " &lt;&lt; username &lt;&lt; " " &lt;&lt; password &lt;&lt; " " &lt;&lt; name &lt;&lt; " "
          &lt;&lt; email &lt;&lt; " " &lt;&lt; telephone &lt;&lt; " " &lt;&lt; address  &lt;&lt; " " &lt;&lt; 1;
  cout &lt;&lt; "\nUser added successfully\n\n";
  
  outFile.close();
  </code></pre>
  
  <p>So, how can I increment the value for each record on insertion and how then target a specific record in the file?</p>
  
  <p>EDIT: I've got as far as being able to display each line:</p>
  
  <pre><code>  if (inFile.is_open())
      {
      while(!inFile.eof())
      {
  
      cout&lt;&lt;endl;
      getline(inFile,line);
      cout&lt;&lt;line&lt;&lt;endl;
  
      }
      inFile.close();
      }
  </code></pre>