title: C++ File input and output:cannot input
body: <p>The following C++ code is to read a txt file and then write the numbers of chars in each line and the number of all chars in the txt file.I use MS Visual Studio 2008.But something is wrong.Only the number of all chars is input into the txt files,<strong>but the numbers of each line are not input into the txt files</strong>.Now I cannot figure it out.Could someone give me some advice?Thanks a lot!</p>
  
  <p>And my another question is what should I do to <strong><em>insert something in the middle of the txt file?</em></strong></p>
  
  <p>This is my Code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
  fstream inOut("copy.txt",ios::in | ios::out | ios::app);
  
  if (!inOut)
  {
      cerr &lt;&lt; "ERROR:cannot open file!" &lt;&lt; endl;
      return -1;
  }
  
  int cnt = 0;
  char ch;
  
  inOut.seekg(0);
  
  while(inOut.get(ch))
  {
      cout.put(ch);
      ++cnt;
      if ('\n' == ch)
      {
          ios::pos_type mark = inOut.tellg();
  
          if (!inOut)
          {
              cerr &lt;&lt; "ERROR!" &lt;&lt; endl;
              return -1;
          }
          inOut &lt;&lt; cnt;
          inOut.put(' ');
  
          inOut.seekg(mark);
      }
  }
  inOut.clear();
  inOut &lt;&lt; cnt &lt;&lt; endl;
  cout &lt;&lt; "[" &lt;&lt; cnt &lt;&lt; "]" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The txt file before running:
  <img src="https://i.stack.imgur.com/DbomY.jpg" alt="enter image description here"></p>
  
  <p>The txt file after running:
  <img src="https://i.stack.imgur.com/pQrxW.jpg" alt="enter image description here"></p>
  
  <p>The result in command line:
  <img src="https://i.stack.imgur.com/WRL95.jpg" alt="enter image description here"></p>