title: Why can't I read from a reference on stream returned by function?
body: <p>I want to get data from a stream that my function returned, but it throws an exception when I try to. Here is my code in short form:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;  
  using namespace std;
  ifstream&amp; detect() {
      ifstream stream;
      stream.open(&quot;in.txt&quot;);
      return stream;
  }
  int main() {
      int s;
      ifstream&amp; fin = detect();
      fin &gt;&gt; s; //exception thrown: Access violation reading
      cout &lt;&lt; s;
  }
  </code></pre>
  <p>How can I solve this problem? Is it caused because of the reference type of the &quot;fin&quot;?</p>