我目前填補這樣一個類(這是相當緩慢的,我的目的)填充在一個匆忙的一類:C++從文件
void CBinLoader::LoadMatchesFromCompiledDat(clsMatches &uMatches)
{
int size = 0;
fread(&size,sizeof(int),1,m_infile);
for(int i = 0; i < size; i++)
{
MatchNode newMatch;
newMatch.EnteredCharacter = ReadWStringFromCompiledDat(m_infile);
int PossibleResults_size = 0;
fread(&PossibleResults_size,sizeof(int),1,m_infile);
for(int j=0; j<PossibleResults_size; j++)
{
PossibleResult pr;
fread(&pr.LenToUseFromEnteredString, sizeof(int), 1, m_infile);
pr.Trans = ReadWStringFromCompiledDat(m_infile);
pr.NextChars = ReadWStringFromCompiledDat(m_infile);
pr.PrevTrans = ReadWStringFromCompiledDat(m_infile);
pr.PrevPrevTrans = ReadWStringFromCompiledDat(m_infile);
fread(&pr.SequenceID, sizeof(int), 1, m_infile);
newMatch.PossibleResults.push_back(pr);
}
uMatches.Content().push_back(newMatch);
}
}
wstring CBinLoader::ReadWStringFromCompiledDat(FILE *pFile)
{
//read the length of the string
int len = 0;
fread(&len, sizeof(int), 1, m_infile);
//make buffer with this length
wchar_t* pBuffer = NULL;
pBuffer = new wchar_t[len+1];
memset(pBuffer, 0, (len+1)*sizeof(wchar_t));
//read the string into the buffer
fread(pBuffer, sizeof(wchar_t), len, m_infile);
wstring result = pBuffer;
delete pBuffer;
return result;
}
是否有可能只是讀它在一個匆忙而不是一片一片地填充它?
你如何確定慢度?花很長時間不同於緩慢。 – 2013-05-08 06:45:41
這並不重要,但是你可以跳過動態分配並簡單地將你的'std :: wstring'調整爲先前讀取的大小,然後簡單地加載並返回它。不需要'new wchar_t []'也不需要後面的'delete [] pBuffer;'(你根本不用做,而是選擇'delete pBuffer';這本身就是一個bug)。 – WhozCraig 2013-05-08 06:53:49
如果我能夠一口氣讀完它,它比填充它更快,就像我的方法一樣。 – tmighty 2013-05-08 06:54:04