How to read multiple binary files in one array

vector<unsigned char> readFile(int count, ...) {
    va_list argptr;
    va_start(argptr, count);
    
    vector<unsigned char> data;
    for (int i = 0; i < count; i++) {
        char* name = va_arg(argptr, char*);
        
        streampos begin, end;
        ifstream file(name, ios::binary|ios::in|ios::ate);
        if (file.is_open()) {
            int size = int(file.tellg());
            file.seekg(0, ios::beg);
            char* memblock = new char[size];
            file.read(memblock, size);
            data.insert(data.end(), memblock, memblock + size);
            delete[] memblock;
        }
        else
            cout << "Unable to open file: \"" << name << "\"" << endl;
    }
    
    return data;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>