title: rdstate() function returning value 3 for code where is used eof() function in file Handling C++
body: <p><a href="https://i.stack.imgur.com/hK4wX.jpg" rel="nofollow noreferrer">My code in Visual Studio</a></p>
  <p>In my code i used eof() then i used rdstate() function to apply condition of getting to know if i have reached end of file so that i can clear() state and reset pointer position to somewhere else than eof(). One more thing i have used XOR ^ operator for condition so if both are true result be false i.e what i want.
  But it is not happening so kindly let me know where i am wrong.</p>
  <p><em>File name is ALi.txt and contains 7 lines of text.</em></p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  int main()
  {
  
  {
      std::fstream file;
      file.open(&quot;Ali.txt&quot;,std::ios::in);
      
      if (file.is_open()) {
  
          std::cout &lt;&lt; &quot;The File has been opened Successfully &quot; &lt;&lt; &quot;\n&quot;;
  
  
          std::string str;
  
          /* Fail()function can also be replaced with good ufucntion which returns true if everything is working fine | False otherwise */
        
          while (!file.eof()) {
              
              file &gt;&gt; str;
              std::cout &lt;&lt; str &lt;&lt; &quot;\n&quot;;
          }
          std::cout &lt;&lt; std::ifstream::eofbit &lt;&lt; &quot;\n&quot; &lt;&lt; std::fstream::failbit &lt;&lt; &quot;\n&quot; &lt;&lt; std::fstream::failbit &lt;&lt; &quot;\n&quot;;
          
          /*If a bad state happened like above then you need to clear it using clear() function to work further like Reading through File*/
  
          //std::cout &lt;&lt; filehandler.good() &lt;&lt; &quot;\n&quot;;            // It is returning 0 | False here. Now to check which of above fu=nction is causing it 
          /*using rdstate() function. fail(4) , eof(2) and Bad(1) bytes*/
  
          std::cout &lt;&lt; (file.rdstate() ^ std::ifstream::eofbit) &lt;&lt; &quot;\n&quot;;
          if ((file.rdstate() ^ std::ifstream::eofbit) == 0) {
              std::cout &lt;&lt; &quot;It is an indicator that we have reached the end of file So now we should do something to go back to begining of file &quot; &lt;&lt; &quot;\n&quot;;
              file.clear();
          }
          file.close();
      }
      else {
          std::cout &lt;&lt; &quot;The File has been Failed to open&quot; &lt;&lt; &quot;\n&quot;;
      }
  }
  </code></pre>
  <p>}</p>