读/写操作既不好也不坏

Read/write operation works neither good nor bad

本文关键字:操作      更新时间:2023-10-16

我正在编程一个人脸检测算法。在我的代码中,我解析一个XML文件(以递归的方式,解析整个XML文件需要大约4分钟的时间,效率非常低)。我想使用Iosteam二进制文件将XML内容保存到一个文件中。我使用C++中的一个结构来使用原始数据。

我的目标是在原始数据文件不存在的情况下仅解析XML

方法是这样工作的:

  1. 如果原始数据文件不存在,请解析XML文件并将数据保存到文件中
  2. 如果存在原始数据文件,请从该文件中读取原始数据

我的问题是:每当我打开原始数据文件并从中读取时。我只能从文件中读取少量字节,我不知道读取了多少,但在某一点上,我的缓冲区中只接收到0x00数据。

我的猜测是:我认为这与操作系统缓冲区有关,它有一定的缓冲区用于读写操作。我可能错了。虽然我不确定哪一个操作不好,但它要么是写的,要么是读的。

我想一个字符一个字符或一行行一行地写/读原始数据。另一方面,该文件不包含文本,这意味着我无法逐行或逐字符读取。

原始数据大小为

   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("Completednn"); 
    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 filen"); 
    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("nCompletednn"); 
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;
    }

尝试使用Boost IOstreams库。它有一个易于使用的wrraper文件处理