user2614596
user2614596

Reputation: 630

How to save/read arrays of data in a file (C++)

i'm using a code which computes SIFT descriptors from an image. I have to process each frame from a video and store the sequence of each computed descriptor. For each image the descriptor is made by two arrays:

Frames = (double*)realloc(Frames, 4 * sizeof(double)* nframes); 
Descr = (vl_uint8*)realloc(Descr, 128 * sizeof(vl_uint8)* nframes);

how can i save the sequence of these two arrays in a file and then recover these data (for each frame of the video) from the file?

Upvotes: 0

Views: 334

Answers (2)

GPPK
GPPK

Reputation: 6666

You can use ofstream to write to a file. The below code, which probably doesn't work straight out the can, should point in the right direction.

#include <fstream>
#include <iterator>
#include <algorithm>

void writeDescriptors() {

    std::ofstream output( "C:\\descriptors.txt", std::ios::binary );
    for ( int i = 1 ; i < Frames.size(); i++ )
    {
        out << ",  " << Frames[i];
    }
}

In order to read the descriptors back in, simply use ifstream and reverse the algorithm

Upvotes: 2

tebe
tebe

Reputation: 316

Try looking for fwrite and fread.

If you have a variable number of frames (nframes) it might help to use the first 2*4bytes in the file to store the exact number of frames the file contains.

EDIT: http://www.cplusplus.com/reference/cstdio/fwrite/

Upvotes: 0

Related Questions