2012-04-05 5 views
0

私のプログラミングでは、顔検出アルゴリズム。
私のコードでは、XMLファイルを解析しています(再帰的に非常に非効率的ですが、XMLファイル全体を解析するのに約4分かかります)。 Iosteamをバイナリファイルに保存してXMLコンテンツを保存したいとします。 私は生データを使用するためにC++で構造体を使用しています。fstream |読み書き操作は良いことも悪いこともしない|大きなファイルへの読み込み

私の目標は、生データファイルが存在しない場合は、XML のみを解析することです。です。

このメソッドは次のように動作します。 1.生データファイルが存在しない場合: XMLファイルを解析し、データをファイルに保存します。 2.生データファイルが存在する場合 - ファイルから生データを読み込みます。

問題は次のとおりです。生データファイルを開いて読み込むときはいつでも。 ファイルから少量のバイトしか読み込めませんが、どれくらいの量があるのか​​わかりませんが、特定のポイントでは私のバッファには0x00のデータしか入っていません。

私の推測:これはOSのバッファと関係があります。これには、読み書き操作に一定量のバッファがあります。私はこれについて間違っているかもしれない。操作のどちらがうまくいかないかわからないのですが、それは書き込みか読み取りのどちらかです。

私はcharまたは行ごとにrawデータcharを書き込むことを考えていました。一方、ファイルにはテキストは含まれていません。つまり、行単位で読み込むことも、char型で読み込むこともできません。

生のデータサイズは、この関数でretriveある

size_t datasize = DataSize(); == 196876 (Byte) 

ある

/* Get the upper bound for predefined cascade size */ 
size_t CCacadeInterpreter::DataSize()   
{ 
// this is an upper boundary for the whole hidden cascade size 
size_t datasize = sizeof(HaarClassifierCascade) * TOTAL_CASCADE+  
    sizeof(HaarStageClassifier)*TOTAL_STAGES + 
    sizeof(HaarClassifier) * TOTAL_CLASSIFIERS + 
    sizeof(void*)*(TOTAL_CASCADE+TOTAL_STAGES+TOTAL_CLASSIFIERS); 

return datasize; 
} 

この

BYTE * CCacadeInterpreter::Interpreter() 
{ 
printf("|Phase - Load cascade from memory | CCacadeInterpreter::Interpreter | \n"); 

size_t datasize = DataSize(); 
// Create a memory structure 
nextFreeSpace = pStartMemoryLocation = new BYTE [datasize]; 
memset(nextFreeSpace,0x00,datasize); 

// Try to open a predefined cascade file on the current folder (instead of parsing the file  again) 
fstream stream; 
stream.open(cascadeSavePath); // ...try existing file   
if (stream.is_open()) 
{ 
    stream.seekg(0,ios::beg); 
    stream.read((char*)pStartMemoryLocation , datasize); // **ream from file** 
    stream.close(); 
    printf("|Load cascade from saved memory location | CCacadeInterpreter::Interpreter | \n"); 
    printf("Completed\n\n"); 
    stream.close(); 
    return pStartMemoryLocation; 
} 

     // Open the cascade file and parse the cascade xml file 
std::fstream cascadeFile; 
cascadeFile.open(cascadeDestanationPath, std::fstream::in);  // open the file with read only attributes 
if (!cascadeFile.is_open()) 
{ 
    printf("Error: couldn't open cascade XML file\n"); 
    delete pStartMemoryLocation; 
    return NULL; 
} 

// Read the file XML file , line by line 
string buffer, str; 
getline(cascadeFile,str); 
while(cascadeFile) 
{ 
    buffer+=str; 
    getline(cascadeFile,str); 
} 
cascadeFile.close(); 
split(buffer, '<',m_tokens); 

// Parsing begins 
pHaarClassifierCascade = (HaarClassifierCascade*)nextFreeSpace; 
nextFreeSpace += sizeof(HaarClassifierCascade); 
pHaarClassifierCascade->count=0; 
pHaarClassifierCascade->orig_window_size_height=20; 
pHaarClassifierCascade->orig_window_size_width=20; 

m_deptInTree=0; 
m_numOfStage = 0; 
m_numOfTotalClassifiers=0; 
while (m_tokens.size()) 
{ 
    Parsing(); 
} 
// Save the current cascade into a file 
SaveBlockToMemory(pStartMemoryLocation,datasize); 
printf("\nCompleted\n\n"); 
return pStartMemoryLocation; 
    } 

    bool CCacadeInterpreter::SaveBlockToMemory(BYTE * pStartMemoryLocation,size_t dataSize) 
    { 
fstream stream; 
if (stream.is_open()) 
    stream.close(); 

stream.open(cascadeSavePath); // ...try existing file   
if (!stream.is_open()) // ...else, create new file... 
    stream.open(cascadeSavePath, ios_base::in | ios_base::out | ios_base::trunc); 

stream.seekg(0,ios::beg); 
stream.write((char*)pStartMemoryLocation,dataSize); 
stream.close(); 
return true; 
    } 

ヘルプは非常に高く評価されるだろう S

のような方法で作業

答えて

0

Boost IOstreamsライブラリを使用してみてください。 ファイルハンドリングのための使いやすいラスターです

関連する問題