How to read a file stream entirely into a vector?

This is a discussion on How to read a file stream entirely into a vector? within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    21,415
    Have you considered using formatted input instead of a typecast?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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,262
    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, 09: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, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21