我使用MapVirtualFile使用C ++ VS2010在Window下映射文件。 空白是
void* m_pVoiceData;
我现在想用数据填充结构的向量。
结构是
struct udtPitchmark
{
unsigned long ByteStart;
unsigned long ByteCount;
unsigned long F0;
};
struct udtPitchmarks
{
vector<udtPitchmark>Pitchmarks;
};
我一直在使用
void clsMapping::FeedPitchmarksFromMap(udtPitchmarks &uAudioInfo,unsigned long int uBytePos)
{
unsigned int iCountPM;
memcpy(&iCountPM, &((char*)(m_pVoiceData))[uBytePos],sizeof(unsigned long));
uAudioInfo.Pitchmarks.resize(iCountPM);
for (unsigned int i=0;i<iCountPM;i++)
{
iBytePos+=sizeof(unsigned long);
unsigned long int iByteStart;
memcpy(&iByteStart, &((char*)(m_pVoiceData))[iBytePos],4);
iBytePos+=sizeof(unsigned long);
unsigned long int iByteCount;
memcpy(&iByteCount, &((char*)(m_pVoiceData))[iBytePos],4);
iBytePos+=sizeof(unsigned long);
unsigned int iF0;
memcpy(&iF0, &((char*)(m_pVoiceData))[iBytePos],4);
uAudioInfo.Pitchmarks[i].ByteStart =iByteStart;
uAudioInfo.Pitchmarks[i].ByteCount =iByteCount;
uAudioInfo.Pitchmarks[i].F0 =iF0;
}
}
每个音标都有3个memcpy。 但我希望我能一次阅读所有的音标。 但是我不知道该怎么做。
存储的结构如下:
vector<udtPitchmarks>::iterator it = nPitchmarks.Content().begin();
for (;it != nPitchmarks.Content().end(); ++it)
{
unsigned int iCountPMs = it->Pitchmarks.size();
fwrite(&iCountPMs,sizeof(unsigned int),1,outfile);
vector<udtPitchmark>::iterator it2 = it->Pitchmarks.begin();
for(;it2 != it->Pitchmarks.end(); ++it2)
{
fwrite(&it2->ByteStart,sizeof(unsigned long),1,outfile);
fwrite(&it2->ByteCount,sizeof(unsigned long),1,outfile);
fwrite(&it2->F0,sizeof(unsigned long),1,outfile);
}
}
有人可以帮忙吗?
答案 0 :(得分:2)
你可以一次阅读整个udtPitchmark结构
m_pVoiceData = (char *)m_pVoiceData + sizeof(unsigned int); // size field
memcpy( &uAudioInfo.Pitchmarks[i], (udtPitchmark *)m_pVoiceData + i, sizeof udtPitchmark );
或整个载体:
memcpy( &uAudioInfo.Pitchmarks[0], (udtPitchmark *)m_pVoiceData, sizeof(udtPitchmark ) * iCountPM);
答案 1 :(得分:0)
如果您将数据作为数组放置,您可以一次性写入数据。访问器功能可以帮助您维护可读的界面:
struct Pitchmark
{
unsigned long data[3];
unsigned long & start() { return data[0]; }
unsigned long & count() { return data[1]; }
unsigned long & f0() { return data[2]; }
unsigned char * ptr() { return static_cast<unsigned char *>(data); }
};
Pitchmark pm;
memcpy(pm.ptr(), m_pVoiceData, sizeof pm.data);