(C++) Loading a file into a vector
        Posted  
        
            by 
                Alden
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Alden
        
        
        
        Published on 2012-10-13T20:36:28Z
        Indexed on 
            2012/10/13
            21:37 UTC
        
        
        Read the original article
        Hit count: 212
        
This is probably a simple question, however I am new to C++ and I cannot figure this out. I am trying to load a binary file and load each byte to a vector. This works fine with a small file, but when I try to read larger than 410 bytes the program crashes and says:
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
I am using code::blocks on windows.
This is the code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
    std::vector<char> vec;
    std::ifstream file;
    file.exceptions(
        std::ifstream::badbit
      | std::ifstream::failbit
      | std::ifstream::eofbit);
    file.open("file.bin");
    file.seekg(0, std::ios::end);
    std::streampos length(file.tellg());
    if (length) {
        file.seekg(0, std::ios::beg);
        vec.resize(static_cast<std::size_t>(length));
        file.read(&vec.front(), static_cast<std::size_t>(length));
    }
    int firstChar = static_cast<unsigned char>(vec[0]);
    cout << firstChar <<endl;
    return 0;
}
Thank you for your help!
© Stack Overflow or respective owner