1

start sequenz char[9] と 5 つの ID の char[5] を含むバイナリ ファイルを読みたいと思います。ファイルを開いたのですが、データを正しく保存する方法がわかりません。

char[8] start_sq = "STARTSEQ\n" // start of the binary file 

その後、5 つの ID があります。

start_sq の後に開始位置を設定するにはどうすればよいですか

int current_pos = 0;
std:ifstream readFile_;
int *id;
while( (current_pos = (readFile_.tellg())) == eof) 
{
   //start after start_sq // not sure how to
   int tmp_id = readFile_.read(reinterpret_cast<char*>(&id), sizeof(int)); // should be first ID (OR?)
  ids.push_back(tmo_id);
  // again for ID 2 

}

私の質問が最初は少し不明確である場合、私はそれを理解しています。しかし、それを正しく実装する方法がわかりません。しかし、ご覧のとおり、いくつかのアイデア/アプローチがあります。

任意の助けのためのthx :)

4

1 に答える 1

1

はい、次のようにします。

[警告: 以下はまったくテストされていません! ]

//int current_pos = 0;
std:ifstream readFile_;

... // Open the file in binary mode, etc...

//int *id;
char id;

// Read the 'STARTSEQ' string + 1 carriage return :
char[9] startseq;
readFile_.read(reinterpret_cast<char*>(&startseq[0]),  9);
//                                                    ^^^
// IMPORTANT : The above line shifts the current_pos of 9 bytes.
// Short : readFile_.read(startseq, sizeof(startseq));

// Then read your IDs
// You want your IDs as chars so let's read chars, not int.
while( readFile_.good() ) // or while( !readFile_.eof() ) 
{
   readFile_.read(reinterpret_cast<char*>(&id), sizeof(char));
   // IMPORTANT : The above line shifts the current_pos of 1 byte.
   // Short : readFile_.read(&id, 1);
   ids.push_back(id);
}
// The above 'while' loops until EOF is reached (aka. 5 times). 
// See ifstream.good(), ifstream.eof().

注 : 読み取る文字列 ("STARTSEQ\n") は、8 文字ではなく 9 文字です。

idsベクトルを設定するための別のアプローチは次のとおりです。

vector<char> ids;
int size = 5;
ids.resize(size);
// Read 'size' bytes (= chars) and store it in the 'ids' vector :
readFile_.read(reinterpret_cast<char*>(&ids[0]), size);

注:whileここでは使用しませんが、注意: EOF に達したかどうかをチェックしません。

それがあなたが求めているものであることを願っています。

于 2013-06-18T23:13:57.780 に答える