I want to open a file, read the first 5 bytes, check that the first 4 match a given signature and the 5th is the size of my file header. The size of the header is what i am surposed to read next to get the build of the rest of the data.
What ive tried so far:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
typedef struct file_header{
char signature[4];
unsigned char size;
char version;
//Different other things left out for example matters.
unsigned int blocks;
unsigned int block_size;
} header;
void open_file(std::string fileName, header &header){
std::ifstream fIn;
fIn.exceptions(std::ifstream::failbit | std::ifstream::badbit);
char buffer[5];
try{
fIn.open(fileName, std::ios::binary);
//read the 1st 5 bytes to get header signature and header size
fIn.read(buffer, 5);
header.size = buffer[4];
//Here my problems begins...
fIn.seekg(0, std::ios::beg);
std::vector<char*> header_data;
header_data.reserve((int)header.size);
//How do i read the first (int)header.size bytes of file to Vector?
fIn.read(header_data[0], (int)header.size); //Wont work?
}catch(std::ifstream::failure ex){
std::cerr << ex.code().message() << std::endl;
exit(0);
}
}
int main(int argc, char* argv[]){
char* filename = argv[1];
file_header header;
open_file(filename, header);
return 0;
}
I have just started with c++ not so long ago, but in Java, i can do something neat like:
char header_size[(int)header.size];
But what i found out so far, is that you can't make dynamic arrays in c++ therefore the vector.
What can i do to get the output i need?
The code ive written give a Vector out of range, i guess the [0] does that?
Any help or pointers appreceated..