title: Changing a string in the binary file
body: <p>I'm working on a system in which you add the details of apartment and then later on customers will be able to see the list and book any of it. So I had to define a way to make booked apartments unavailable, I've defined a function that generates random numbers, and I've added a string with the value of 'available' to it, so each time it saves a record to the file, next to the id, you can see the string available, like this: </p>
  
  <pre><code>    string available = "  : available";
  
      cout &lt;&lt; "Auto-Generated ID:" &lt;&lt; randomNum() &lt;&lt; endl &lt;&lt; endl;
      auto_id = to_string(randomNum()) + available;
  </code></pre>
  
  <p>this works totally fine so far, but on the other hand, when user books an apartment, I've defined another function that is supposed to go through the same file, and change this available to unavailable, well I've defined a string called unavailable with the value of 'unavailable' and a replacing string function like this : </p>
  
  <pre><code>    void change_status(string available, string unavailable) {
      std::ifstream ifile("apartment.txt", std::ios::binary | ios::app);
      ifile.seekg(0, std::ios_base::end);
      long s = ifile.tellg();
      char *buffer = new char[s];
      ifile.seekg(0);
      ifile.read(buffer, s);
      ifile.close();
      std::string txt(buffer, s);
      delete[] buffer;
      size_t off = 0;
      while ((off = txt.find(available, off)) != std::string::npos)
          txt.replace(off, sizeof(available)-1, unavailable);
      std::ofstream ofile("apartment.txt");
      ofile.write(txt.c_str(), txt.size());
  
      }
  </code></pre>
  
  <p>and this is how I call the function; </p>
  
  <pre><code>        string unavailable = "  : unavailable"; 
  
          cout &lt;&lt; "Enter In Apartment ID" &lt;&lt; endl;
  
          cin &gt;&gt; booker_apartment_id;
  
          serachi = to_string(booker_apartment_id) + unavailable;
  
          change_status(auto_id, serachi);
  </code></pre>
  
  <p>but when the function is done, and I try to display data's to check if it's changed to unavailable, it ends up with this error; </p>
  
  <pre><code>       Unhandled exception at 0x0FCB7EA6 (msvcp120d.dll) in Assignment.exe: 0xC0000005: Access violation reading location 0x006FEF28.
  </code></pre>
  
  <p>I'm an amateur, and this is my second project in c++, any help to fix this, or either any suggested method for doing such a thing, is pretty much appreciated.</p>
  
  <p><strong>EDIT</strong>: This is where my debugger is referring to ; </p>
  
  <pre><code>        if (_State == ios_base::goodbit
          &amp;&amp; _Ostr.rdbuf()-&gt;sputn(_Str.c_str(), (streamsize)_Size)
          != (streamsize)_Size)  // an arrow appears next to this line after breaking.... 
          _State |= ios_base::badbit;
          else
          for (; 0 &lt; _Pad; --_Pad)    // pad on right
          if (_Traits::eq_int_type(_Traits::eof(),
          _Ostr.rdbuf()-&gt;sputc(_Ostr.fill())))
          {   // insertion failed, quit
          _State |= ios_base::badbit;
          break;
          }
         _Ostr.width(0);
         _CATCH_IO_(_Ostr)
          }
  </code></pre>