ファイルを開き、最初の5バイトを読み、最初の4つが与えられた署名に一致し、5番目が私のファイルヘッダのサイズであることを確認します。ヘッダーのサイズは、残りのデータのビルドを取得するために次に読んだものです。ベクトルの未知の部分を読む
#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;
}
私はただではないので、ずっと前に、C++で開始しているが、Javaで、私のようなきちんとした何かを行うことができます:
char header_size[(int)header.size];
しかし、私が発見し、これまでに試した何アイブ
今のところ、あなたはC++で動的配列を作ることができないので、ベクトルです。
私は必要な出力を得るために何ができますか?
書かれたコードは範囲外ですが、[0]はそれをしていると思いますか?
すべてのヘルプやポインタappreceated ..
がベクトルの宣言に変更して終了したら、あなたは header_data;'リサイズする準備を'fIn.read(&header_data [0]、(int)header.size);'それはそれでした!ありがとう! –
FastDuck
header_data
に割り当てられたメモリを解放する必要があります: 'のstd ::ベクトル