Ildon
Ildon

Reputation: 47

Read 2D vector from binary File C++

i have some Problem reading a 2D Vector from a binary File:

For Example:

My Binary File is structured like this :

243524
764756
746384

Now i want to create a 2D Vector which looks exactly like the bin File.

What i've done so Far:

I created a 1D Vector which contains all Elements. Then i created a Loop and filled the 2D Vector.

My Problem ist that i have a huge .bin File and the for loop cost a lot of Time. is there a possibility to get the 2DVector faster ?

My Code :

ifstream file("C:\\XXX.bin", ios::in | ios::binary | ios::ate);
char * buffer;
long size;
file.seekg(0, std::ios::end); 
size = file.tellg();  

buffer = new char[size];
file.read(buffer, size);
file.close();   


double* double_values = (double*)buffer;//reinterpret as doubles
vector<double> buffer2(double_values, double_values + (size / sizeof(double)));

//cout << "Filling matrix with test numbers.";
int h = 0;
for (int i = 0; i < (size / sizeof(double)) / row; i++)
{
    vector<double> temp;
    for (int j = 0; j < row; j++)
    {
        if (h<size / sizeof(double))
        {
            temp.push_back(buffer2[h]);
            h++;
        }

    }

    bin_file.push_back(temp);
}

Hope that sb can Help me :)

Upvotes: 0

Views: 1227

Answers (1)

user4581301
user4581301

Reputation: 33932

Suggestion: eliminate the copying by reading directly into the vector with std::vector::data

ifstream file("C:\\XXX.bin", ios::in | ios::binary | ios::ate);
char * buffer;
long size;
file.seekg(0, std::ios::end); 
size = file.tellg();  
//allocate a properly sized vector of doubles. Probably should test that size 
// is evenly divisible
vector<double> buffer(size/sizeof(double));
// read into the vector of doubles by lying and saying it's an array of char 
file.read((char*)buffer.data(), size);
file.close();   

Note that the vector is 1D. Access to this vector requires a bit of math. You could

buffer[map2dto1d(row, column)];

where

size_t map2dto1d(size_t row, size_t column)
{
    return row * numberColumns + column;
}

but you are better off wrapping the vector in a class that stores the row and column information along with a properly sized vector and forces the user to index correctly.

Something like this:

class Matrix
{
private:
    size_t rows, columns;
    std::vector<double> matrix;
public:
    // zero initialized matrix of row by column
    Matrix(size_t numrows, size_t numcols):
        rows(numrows), columns(numcols), matrix(rows * columns)
    {
         // can place file reading logic here, but make sure the file matches 
         // numrows * numcols when reading and throw an exception if it doesn't
    }

    // matrix of row by column using provided in vector. in will be 
    // consumed by this constructor 
    // make sure in is big enough to support rows * columns
    Matrix(size_t numrows, size_t numcols, std::vector<double> && in):
        rows(numrows), columns(numcols), matrix(std::move(in))
    {

    }

    double & operator()(size_t row, size_t column)
    {
        // check bounds here if you care
        return matrix[row * columns + column];
    }

    double operator()(size_t row, size_t column) const
    {
        // check bounds here if you care
        return matrix[row * columns + column];
    }

    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }

};
// convenience function for printing matrix
std::ostream & operator<<(std::ostream &  out, const Matrix & in)
{
    for (int i = 0; i < in.getRows(); i++)
    {
        for (int j = 0; j < in.getColumns(); j++)
        {
            out << in(i,j) << ' ';
        }
        out << std::endl;
    }

    return out;
}

Upvotes: 1

Related Questions