title: How to update struct item in binary files
body: <p>I have a binary file that i write some struct items to it. Now I want to find and update specific item from file items. 
  Note that my struct has a vector and its size is not constant.</p>
  
  <p>my struct:</p>
  
  <pre><code>struct mapItem
  {
      string term;
      vector&lt;int&gt; pl;
  };
  </code></pre>
  
  <p>codes that write struct items to file</p>
  
  <pre><code>if (it==hashTable.end())//didn't find
              {
                  vector &lt;int&gt; posting;
                  posting.push_back(position);                
                  hashTable.insert ( pair&lt;string,vector &lt;int&gt; &gt;(md,posting ) );
                  mapItem* mi = new mapItem();
                  mi-&gt;term = md;         
                  mi-&gt;pl = posting;
                  outfile.write((char*)mi, sizeof(mi));
  
              }
              else//finded
              {
  
              }
  </code></pre>
  
  <p>In else block I want to find and update item with its term(term is unique).</p>
  
  <p>Now I have changed my code like this to serialize my vector. </p>
  
  <pre><code>if (it==hashTable.end())//didn't find
              {
                  vector &lt;int&gt; posting;
                  posting.push_back(position);                
                  hashTable.insert ( pair&lt;string,vector &lt;int&gt; &gt;(md,posting ) );
                  mapItem* mi = new mapItem();
                  mi-&gt;term = md;         
                  mi-&gt;pl = posting;
  
                  if(!outfile.is_open())
                      outfile.open("sample.dat", ios::binary | ios::app);
  
                  size_t size = mi-&gt;term.size() + 1;
                  outfile.write((char*)&amp;size, sizeof(size) );
                  outfile.write((char*)mi-&gt;term.c_str(), size);
                  size = (int)mi-&gt;pl.size() * sizeof(int);
                  outfile.write((char*)&amp;size, sizeof(size) );
                  outfile.write((char*)&amp;mi-&gt;pl[0], size );
  
                  outfile.close();
              }
              else//finded
              {
  
                  (it-&gt;second).push_back(position);
  
                  mapItem* mi = new mapItem();
  
                  size_t size;
  
                  if(!infile.is_open())
                  {
                      infile.open("sample.dat", ios::binary | ios::in);
                  }
  
                  do{
                      infile.read((char*)&amp;size, sizeof(size) ); // string size
                  mi-&gt;term.resize(size - 1); // make string the right size
                  infile.read((char*)mi-&gt;term.c_str(), size); // may need const_cast
                  infile.read((char*)&amp;size, sizeof(size) ); // vector size
                  mi-&gt;pl.resize(size / sizeof(int));
                  infile.read((char*)&amp;mi-&gt;pl[0], size );
                  }while(mi-&gt;term != md);
  
                  infile.close();
              }
  </code></pre>
  
  <p>Well, my main question still remains: how can I <strong>update</strong> the data that I found?
  Is there a better way to find them?</p>