0

I am currently loading huge amounts of data in memory to quickly access it. Until now I put everything into RAM. But now that my data gets just too huge, I can not do this anymore. I am therefore using a mapped file.

In this file I have stored vectors of "pitchmarks" for an audio file.

struct udtPitchmark
{
    int ByteStart;
    int ByteCount;
};

struct udtAudioInfo
{
    int ByteStart;
    int ByteCount;
    vector<udtPitchmark>Pitchmarks;
};

I have written huge vectors of "udtAudioInfo" to a file like this:

// Serialize
int iSize = nAudioInfos.Content().size();
fwrite(&iSize,sizeof(int),1,outfile);

vector<udtAudioInfo>::iterator it = nAudioInfos.Content().begin();
for (;it != nAudioInfos.Content().end(); ++it)
{
    //we need to know the position that the data will be written to 
    int iStartPos= ftell( outfile );

    fwrite(&it->ByteStart,sizeof(int),1,outfile);
    fwrite(&it->ByteCount,sizeof(int),1,outfile);
    int len = it->Pitchmarks.size();
    fwrite(&len,sizeof(int),1,outfile);

    vector<udtPitchmark>::iterator it2 = it->Pitchmarks.begin();
    for(;it2 != it->Pitchmarks.end(); ++it2)
    {
        fwrite(&it2->ByteStart,sizeof(int),1,outfile);
        fwrite(&it2->ByteCount,sizeof(int),1,outfile);
    }
    //and now we need to know the length of the data that was written
    int iEndPos= ftell( outfile );
    int iLen=(iEndPos-iStartPos);
    //now store the file-location info in a map and save this map when we have saved the entire audio info
    nMapping.Add (iStartPos,iLen);
}

The question is if I can easily read one of the udtAudioInfos from the mapped file. I am not sure if it is possible to simply copy such structs from file to a variable. Thank you for the help.

4

1 回答 1

1

If portability and a particular compiler's implementation are not at issue, if some vector<> stores its data in an order known to you, you can accommodate that in reading its file image. The STL objects don't specify how they store their data, however.

If you create a small test, you'll be able to readily examine how your vector's elements are stored and you'll be able to work from there with confidence.

于 2013-10-08T21:23:28.657 回答