title: Following use of getline, cannot write to file
body: <p>I'm building a large file I/O library and am currently struggling with the interoperability of getline() and writing to a file. My question below is alot like this one, which unfortunately remains unanswered: <a href="https://stackoverflow.com/questions/4074628/c-after-i-use-getline-to-read-from-a-file-i-can-no-longer-write-to-the-txt-file">C++ After I use getline to read from a file I can no longer write to the txt file</a></p>
  
  <p>Once I've used getline(), I'm no longer able to write to the file. Read requests with getline() will continue to operate, but write requests will fail. However, if I comment out the usage of getline(), the write operations will succeed.</p>
  
  <p>My code is posted below. I've noticed that the failbit is being activated following the first write attempt. However, the reason for this occurring is unknown to me, as it is not active if I remove the getline() operations.</p>
  
  <p>I should be clear -- I can read from the existing file (which contains TWO lines) perfectly. However, I am unable to write to that file, unless I remove the getline() statements.</p>
  
  <p>Any help, is as always, appriciated.</p>
  
  <pre><code>// Includes
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  // Namespace
  using namespace std;
  
  int main(){
      // filestream object
      fstream workFile("testdoc.txt"); //fstream uses ios::in | ios::out by default
      // note that I have tried including ios::in | ios::out to no avail
  
      // read from file
      string grabbedLine;
      getline(workFile, grabbedLine);
      cout &lt;&lt; "Line #1: " &lt;&lt; grabbedLine &lt;&lt; endl;
  
      // write to file
      workFile&lt;&lt; "Here is some output (#1)" &lt;&lt; endl;
  
      // read from file
      getline(workFile, grabbedLine);
      cout &lt;&lt; "Line #2: " &lt;&lt; grabbedLine &lt;&lt; endl;
  
      // write to file
      workFile&lt;&lt; "Here is some output (#2)" &lt;&lt; endl;
  
      // wait for some input...
      getchar();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>Current console output (as expected from text file):
  Line #1: This is line#1
  Line #2: This is line#2
  </code></pre>