Writing multiple array pointers to file with ofstream?

I'm having some seriously strange trouble writing multiple arrays of data to a file. Basically, I'm wanting to store all the array sizes at the top of the file, and then the array data following. This way I can just read the sizes and use that to construct arrays to hold the data on import, and I'll know exactly where each array begins and ends.

Here's the problem: I write the data, but it's different on import. Please take a look at my little test code. At the bottom there are comments about the values.

Thank you very much, fellow programmers! :)

#include <iostream>
#include <fstream>

int main()
    int     jcount = 100, // First item in file
            kcount = 200, 
            in_jcount,    // Third item in file. jcount is used to find where this ends.

    float   *j = new float[jcount],
            *k = new float[kcount],

    for(int i = 0; i < jcount; ++i) // Write bologna data...
        j[i] = (float)i;
    for(int i = 0; i < kcount; ++i)
        k[i] = (float)i;

    std::ofstream outfile("test.dat");

    outfile.write((char*)&jcount, sizeof(int)); // Good

    outfile.write((char*)&kcount, sizeof(int)); // Good

    outfile.write((char*)j, sizeof(float) * jcount); // I don't know if this works!

    outfile.write((char*)k, sizeof(float) * kcount); // I don't know if this works!


    std::ifstream in("test.dat");

    in.read((char*)&in_jcount, sizeof(int));    // == jcount == 100, good.
    in.read((char*)&in_kcount, sizeof(int));    // == kcount == 200, good.

    in_j = new float[in_jcount],
    in_k = new float[in_kcount];    // Allocate arrays the exact size of what it should be

    in.read((char*)in_j, sizeof(float) * in_jcount);    // This is where it goes bad!
    in.read((char*)in_k, sizeof(float) * in_kcount);

    float   jtest_min = j[0],   // 0.0
            jtest_max = j[jcount - 1],  // this is 99.

            ktest_min = k[0],   // 0.0
            ktest_max = k[kcount - 1],  // this is 200. Why? It should be 199!

            in_jtest_min = in_j[0], // 0.0
            in_jtest_max = in_j[in_jcount - 1], // 99

            in_ktest_min = in_k[0], // 0.0
            in_ktest_max = in_k[in_kcount - 1]; // MIN_FLOAT, should be 199. What is going on here?


    delete k;
    delete j;
    delete in_j;
    delete in_k;


There's nothing obviously wrong with this code (indeed, I don't see the errors you're encountering when I try running it), except for the fact that you are not checking for errors opening the input/output files.

For example, if you don't have permission to write to "test.dat", the open will silently fail, and you'll read back in whatever happened to be in the file before.

I've got the same bug, I fix it by using binary file:

ofstream outfile;
outfile.open ("test.dat", ios::out | ios::binary);


ifstream in;
in.open ("test.dat", ios::in | ios::binary);

Need Your Help

running system calls inside MySql trigger

mysql database triggers system-calls

how can i execute system calls within a trigger?

R + Org-mode: how to avoid naming plot files?

r emacs latex org-mode sweave

When I use Sweave with R, I can avoid explicitly naming the plot files by simply enclosing the plot commands within a code-chunk like &lt;&lt;fig=TRUE&gt;&gt; ... @. The Sweave driver automatically