title: Creating a class with an open file stream
body: <p>I've created a class which is supposed to read in DNA sequences: It contains an if stream private member:</p>
  
  <p>Interface:</p>
  
  <pre><code>class Sequence_stream {
      const char* FileName;
      std::ifstream FileStream;
      std::string FileFormat;
  
  public:
      Sequence_stream(const char* Filename, std::string Format);
      NucleotideSequence get();
  };
  </code></pre>
  
  <p>Implementation:</p>
  
  <pre><code>Sequence_stream::Sequence_stream(const char* Filename, std::string Format)
  {
      FileName = Filename;
      FileStream.open(FileName);
      FileFormat = Format;
      std::cout &lt;&lt; "Filestream is open: " &lt;&lt; FileStream.is_open() &lt;&lt; std::endl;
  }
  
  NucleotideSequence Sequence_stream::get()
  {
      if (FileStream.is_open())
      {
          char currentchar;
          int basepos = 0;
          std::string name;
          std::vector&lt;Nucleotide&gt; sequence;
          currentchar = FileStream.get();
          if (currentchar == '&gt;' &amp;&amp; false == FileStream.eof()) {  // Check that the start of the first line is the fasta head character.
              currentchar = FileStream.get(); // Proceed to get the full name of the sequence. Get characters until the newline character.
              while(currentchar != '\n' &amp;&amp; false == FileStream.eof())
              {
                  if (true == FileStream.eof()) {
                      std::cout &lt;&lt; "The file ends before we have even finished reading in the name. Returning an empty NucleotideSequence" &lt;&lt; std::endl;
                      return NucleotideSequence();
                  }
                  name.append(1, currentchar);
                  currentchar = FileStream.get();
              } // done getting names, now let's get the sequence.
              currentchar = FileStream.get();
              while(currentchar != '&gt;' &amp;&amp; false == FileStream.eof())
              {
                  if(currentchar != '\n'){
                      basepos++;
                      sequence.push_back(Nucleotide(currentchar, basepos));
                  }
                  currentchar = FileStream.get();
              }
              if(currentchar == '&gt;')
              {
                  FileStream.unget();
              }
              return NucleotideSequence(name, sequence);
          } else {
              std::cout &lt;&lt; "The first line of the file was not a fasta format description line beginning with '&gt;'. Are you sure the file is of FASTA format?" &lt;&lt; std::endl;
              return NucleotideSequence();
          }
  
      } else {
          std::cout &lt;&lt; "The filestream is not open..." &lt;&lt; std::endl;
          return NucleotideSequence();
      }
  }
  </code></pre>
  
  <p>However if I test it:</p>
  
  <pre><code>int main()
  {
      std::cout &lt;&lt; "Let's try and read in a sequence!" &lt;&lt; std::endl;
      std::cout &lt;&lt; "First we'll create a stream!" &lt;&lt; std::endl;
      Sequence_stream MyDNAStream("~/Dropbox/1_20dd5.fasta", "fasta");
      std::cout &lt;&lt; "Done!" &lt;&lt; std::endl;
      std::cout &lt;&lt; "Now let's try and get a sequence!" &lt;&lt; endl;
      NucleotideSequence firstsequence = MyDNAStream.get();
      return 0;
  } 
  </code></pre>
  
  <p>I see that the if stream is not open:</p>
  
  <pre><code>Let's try and read in a sequence!
  First we'll create a stream!
  Filestream is open: 0
  Done!
  The filestream is not open...
  logout
  
  [Process completed]
  </code></pre>
  
  <p>Although I thought the constructor function opens the if stream. What do I need to do to correct this so as the object is created and contains an open stream? (I know I'm yet to include a destructor which will close the stream upon destruction of the object).</p>
  
  <p>Thanks,
  Ben.</p>