基本的に、バイナリファイル形式から特定のフィールドを読み取るためのさまざまなフラグを探しているバッファがあります。ファイルをバッファに読み込んでいますが、バッファでフラグを検索するコードを書き始めたとき、すぐに壁にぶつかりました。私はC++の初心者ですが、これが私が持っているものです:
void FileReader::parseBuffer(char * buffer, int length)
{
//start by looking for a vrsn
//Header seek around for a vrns followed by 32 bit size descriptor
//read 32 bits at a time
int cursor = 0;
char vrsn[4] = {'v','r','s','n'};
cursor = this->searchForMarker(cursor, length, vrsn, buffer);
}
int FileReader::searchForMarker(int startPos, int eof, char marker[], char * buffer)
{
int cursor = startPos;
while(cursor < eof) {
//read ahead 4 bytes from the cursor into a tmpbuffer
char tmpbuffer[4] = {buffer[cursor], buffer[cursor+1], buffer[cursor+2], buffer[cursor+3]};
if (strcmp(marker, tmpbuffer)) {
cout << "Found: " << tmpbuffer;
return cursor;
}
else {
cout << "Didn't Find Value: " << marker << " != " << tmpbuffer;
}
cursor = cursor + 4;
}
}
私のヘッダーは次のようになります。
#ifndef __FILEREADER_H_INCLUDED__
#define __FILEREADER_H_INCLUDED__
#include <iostream>
#include <fstream>
#include <sys/stat.h>
class FileReader {
public:
FileReader();
~FileReader();
int open(char *);
int getcode();
private:
void parseBuffer(char *, int);
int searchForMarker(int, int, char[], char *);
char *buffer;
};
#endif
strcmpを使用してvrsnの一致を取り戻すことを期待しますが、結果は次のようになります。
Didn't Find Value: vrsn != vrsn
Found:
私が探しているchar配列を通過した後、2回目のパスでそれを見つけたようです。
関連する16進コード