title: Using fstream write
body: <p>I am trying to look for a line in a specified file and replace it with my line.  I don’t have access to the  library on the machines I’ll be running this on, so I created a custom file.  The trouble seems to be the write call to the fstream object.  I was wondering if any of you can help.  Also, my getline loop stops before reaching the end of the file, and I am not sure why.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  #define TARGET2 "Hi"
  
  using namespace std;
  
  void changeFile(string fileName){
      fstream myStream;
      myStream.open(fileName.c_str(),fstream::in | fstream::out);     
  
      string temp;
      string temp2 = "I like deep dish pizza";    
  
      while(getline(myStream, temp)){
          if(temp == TARGET2){
              cout &lt;&lt; "Match" &lt;&lt; endl;
              myStream.write(temp2.c_str(), 100);
              myStream &lt;&lt; temp2 &lt;&lt; endl;
              cout &lt;&lt; "No runtime error: " &lt;&lt; temp2 &lt;&lt; endl;                  
          }
          cout &lt;&lt; temp &lt;&lt; endl;
      }
      myStream.close();
  }
  
  int main (void){        
      changeFile("Hi.txt");
      return 0;
  }
  </code></pre>
  
  <p>Hi.txt</p>
  
  <pre><code>Hi
  Today is June 18
  I like pizza
  I like pepperoni
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Match
  No runtime error: I like deep dish pizza
  Hi
  </code></pre>