title: C++ Newbie: Passing an fstream to a function to read data
body: <p>I have a text file named <code>num.txt</code> who's only contents is the line <code>123</code>. Then I have the following:</p>
  
  <pre><code>void alt_reader(ifstream &amp;file, char* line){
      file.read(line, 3);
      cout &lt;&lt; "First Time: " &lt;&lt; line &lt;&lt; endl;
  }
  
  int main() {
      ifstream inFile;
      int num;
      inFile.open("num.txt");
      alt_reader(inFile, (char*)&amp;num);
      cout &lt;&lt; "Second Time: " &lt;&lt; num &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>First Time: 123
  Second Time: 3355185
  </code></pre>
  
  <p>Can you help me figure out how to get an fstream that is read in a function still assign the variable in main? I'm doing this because <code>alt_reader</code> really has a lot more to it, but this is the part I'm stuck on. Thanks a lot for the help.</p>
  
  <p>UPDATE:
  Using Bill Oneal's comments, I've written</p>
  
  <pre><code>void alt_reader(ifstream &amp;file, stringstream &amp;str, int n){
      char buffer[n+1];
      file.read(buffer, n);
      buffer[n] = 0;
      str &lt;&lt; buffer;
      cout &lt;&lt; "First Time: " &lt;&lt; buffer &lt;&lt; endl; //First Time: 123
  }
  
  int main() {
      ifstream inFile;
      stringstream strm;
      int num;
      inFile.open("num.txt");
      alt_reader(inFile, strm, 3);
      cout &lt;&lt; "Second Time: " &lt;&lt; num &lt;&lt; endl; //Second Time: 123
  }
  </code></pre>
  
  <p>Thanks. Any critiques with what's there now?</p>