title: How to open a text file
body: <p>I am trying to open a text file, and the code below is my attempt:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main() {
      std::ifstream file;
      file.open("InputFile.txt");
  
      std::string fileOutput;
  
      if (file.is_open())
      {
          while (!file.eof())
          {
              file &gt;&gt; fileOutput;
              std::cout &lt;&lt; fileOutput &lt;&lt; std::endl;
  
          }
      }
      else
      {
          std::cout &lt;&lt; "File failed to open" &lt;&lt; std::endl;
      }
      file.close();
  
      return 0;
      }
  </code></pre>
  
  <p>The text file is located on my desktop, and it only contain two integers.</p>
  
  <p><a href="https://i.stack.imgur.com/21QmV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/21QmV.png" alt="enter image description here"></a></p>
  
  <p>Whenever I run the code above, it will show me the "file failed to open" message. I am completely new to c++, so I really don’t have any idea why my code is not working. So any comments would be appreciated. </p>