title: File Handling:What is the use of peek() function in c++?
body: <p>As I have so much problem while dealing with the eof of a file, whenever I code with <code>fstream</code> and the <code>eof</code> appears I have to clear the stream in order to work with that stream. Although I have searched a lot about the <code>eof</code> and I got the result that I should start using:</p>
  
  <pre><code>fstream file("Filename.txt",ios::in|ios::ate|ios::out);
  char str[80];
  while(file&gt;&gt;str)
  {
      //do the required stuff
  }
  //clear the stream and reuse it
  file.clear();
  file.seekp(0);
  </code></pre>
  
  <p>But I have also read about a function called <code>peek()</code> which is also used for such purposes but I am a little confused in its working and I am not able to apply it in the code. So if anyone could guide me through this.</p>
  
  <p>And I have also heard about a function called <code>putback()</code> what's that??</p>
  
  <p>Edit-1</p>
  
  <pre><code>fstream file("Filename.txt",ios::in|ios::ate|ios::out);
  char str[80];
  while(file&gt;&gt;str)
  {
      //do the required stuff
  }
  //clear the stream and reuse it
  file.clear();
  file.seekp(0);
  //Now do the required writing operation after reading the whole file wherever is required
  //I also want to perform writing operations and if this pattern seems most suitable for me
  </code></pre>