Thread: How to read a file stream entirely into a vector?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Vancouver, Canada
    Posts
    4

    How to read a file stream entirely into a vector?

    I've got a file to read, well, read to a vector<short> m_vVoice;

    we all know that

    1 ) ios_base->ios->istream->ifstream

    2 ) istream& read ( char* s, streamsize n );


    So, I tried the following code


    Code:
    
    ifstream ifs (fn.c_str (), ios::binary);
    
    
    // 1) -- fail
    // copy(istream_iterator<short>(ifs),istream_iterator<short>(),back_inserter(this->m_vVoice));
    
    // 2) -- fail
    // vector<short>::iterator currentPos = this->m_vVoice.begin();
    // ifs.read (reinterpret_cast <char *>(&currentPos), this->m_iVoiceLength * sizeof(short));
    
    // 3) -- works, but too slow
    // http://www.thescripts.com/forum/thread133371.html, clumsy way
    for(int i = 0; i < this->m_iVoiceLength; i++)
    {
    ifs.read( reinterpret_cast <char *> (&this->m_vVoice), sizeof (short) );
    }
    The first two methods are trying to read a whole/entire block of memory directly into vector<short> m_vVoice, but failed; the 3rd method is running, but it actually read data one by one. It's pretty slow, which is not what I expected.

    Can anybody help??

    Best Regards
    JIA Pei

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you considered using formatted input instead of a typecast?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jiapei100 View Post
    The first two methods are trying to read a whole/entire block of memory directly into vector<short> m_vVoice, but failed; the 3rd method is running, but it actually read data one by one. It's pretty slow, which is not what I expected.
    Why not just read all the data in that third example, instead of a single short?

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    #include <fstream>
    #include <vector>
    #include <string>
    #include <algorithm>
    
    int main( void ) {
      std::ifstream in( "shorts.txt", std::ios::binary );
    
      if ( in ) {
        std::vector<short> vs;
        vs.insert( vs.begin(), std::istream_iterator<short>(in), std::istream_iterator<short>() );
        std::copy( vs.begin(), vs.end(), std::ostream_iterator<int>(std::cout, "\n"));
      }
    
      return 0;
    }
    The trick is to use insert.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Assuming your file actually contains shorts rather than text, try something like this:
    Code:
    ifstream inputFile( "shorts.txt", std::ios::binary );
    vector<short> shorts( (istreambuf_iterator<short>( inputFile )), istreambuf_iterator<short>() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM