8

I need some help - next piece of code writes a long double dynamic array into the file

int nx = 10, ny = 10;
long double **data = new long double *[nx]; 
long double **data_read = new long double *[nx]; 
for (int i = 0; i < nx; i++) {
    data[i] = new long double [ny];
    data_read[i] = new long double [ny];
}

data[4][4] = 10.0;
printf("%LF\n", data[4][4]);

FILE *file = fopen("data", "wb");
fwrite(data, nx * ny * sizeof(data), 1, file);
fclose(file);

file = fopen("data", "rb");
fread(data, nx * ny * sizeof(data_read), 1, file );
fclose(file);

printf("%LF\n", data_read[4][4]);

But data[4][4] != data_read[4][4], because after reading from file data_read[4][4]=0.0.

Anybody knows what am I doing wrong?

2 Answers 2

13

You need to write each row in your pointer array individually. A mass write will not work for pointer-to-pointer implementations of a fake 2D array (or nD).

For writing:

for (int i=0; i<nx; ++i)
    fwrite(data[i], sizeof(data[i][0]), ny, file);

For reading:

for (int i=0; i<nx; ++i)
    fread(data[i], sizeof(data[i][0]), ny, file);

Frankly, you're (un)fortunate the process didn't crash outright, as you were writing a bunch of memory addresses to your disk file (which a hex dump would have showed you), and were likely walking off the end of your pointer-array allocation during both operations.

That said, I'd start learning about the standard C++ IO library rather than using C-code in a C++ world (or fix the tag on this question).


Single Block Write/Read

You asked if it is possible to do this as a single block read/write. The answer is yes, but you must allocate the memory contiguously. If you still want a pointer-to-pointer array you can certainly use one. Though I recommend using std::vector<long double> for the data buffer, the following will demonstrate what I refer to:

int main()
{
    int nx = 10, ny = 10;

    long double *buff1 = new long double[nx * ny];
    long double *buff2 = new long double[nx * ny];

    long double **data = new long double *[nx];
    long double **data_read = new long double *[nx];

    for (int i = 0; i < nx; i++)
    {
        data[i] = buff1 + (i*ny);
        data_read[i] = buff2 + (i*ny);
    }

    data[4][4] = 10.0;
    printf("%LF\n", data[4][4]);

    FILE *file = fopen("data.bin", "wb");
    fwrite(buff1, sizeof(*buff1), nx * ny, file);
    fclose(file);

    file = fopen("data.bin", "rb");
    fread(buff2, sizeof(*buff2), nx * ny, file );
    fclose(file);

    printf("%LF\n", data_read[4][4]);

    // delete pointer arrays
    delete [] data;
    delete [] data_read;

    // delete buffers
    delete [] buff1;
    delete [] buff2;
}

Output

10.000000
10.000000

Using a std::vector<> for an RAII Solution

All those allocations can get messy, and frankly prone to problems. Consider how this is different:

#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    int nx = 10, ny = 10;

    // buffers for allocation
    std::vector<long double> buff1(nx*ny);
    std::vector<long double> buff2(nx*ny);

    // holds pointers into original
    std::vector<long double*> data(nx);
    std::vector<long double*> data_read(nx);

    for (int i = 0; i < nx; i++)
    {
        data[i] = buff1.data() + (i*ny);
        data_read[i] = buff2.data() + (i*ny);
    }

    data[4][4] = 10.0;
    std::cout << data[4][4] << std::endl;

    std::ofstream ofp("data.bin", std::ios::out | std::ios::binary);
    ofp.write(reinterpret_cast<const char*>(buff1.data()), buff1.size() * sizeof(buff1[0]));
    ofp.close();

    std::ifstream ifp("data.bin", std::ios::in | std::ios::binary);
    ifp.read(reinterpret_cast<char*>(buff2.data()), buff2.size() * sizeof(buff2[0]));
    ifp.close();

    std::cout << data_read[4][4] << std::endl;

    return 0;
}
2
  • I have changed tag in the question =) But there is any way to mass write array correctly?
    – erthalion
    Jan 19, 2014 at 5:43
  • @erthalion The answer to that lies in setting up the original allocation of data contiguous rather than a series of row-allocations. It is possible, but not quite to straight-forward. It requires a single-block allocation of nx*ny members, then building your pointer array from offsets in that block. At write/read time you use the block; not the pointer array.
    – WhozCraig
    Jan 19, 2014 at 5:50
2

Change your code to (see comments for details):

...
data[4][4] = 10.0;
printf("%Lf\n", data[4][4]); // use %Lf to print long double, not %LF

FILE *file = fopen("data", "wb"); 
for (int i=0; i<nx; ++i) // must write row-by-row as data are not continuous
    fwrite(data[i], sizeof(long double), ny, file); 
      // cannot use sizeof(data) here as data is a pointer here, will always return 4
fclose(file);

file = fopen("data", "rb");
for (int i=0; i<nx; ++i)  // read row-by-row
    fread(data_read[i], sizeof(long double), ny, file); 
      // 1. read to data_read, not data
      // 2. cannot use sizeof(data) here as data is a pointer here, will always return 4
fclose(file);

printf("%Lf\n", data_read[4][4]);  // use %Lf to print long double, not %LF
...

Edit:

If you want to format the data in a continuous memory, use vector<long double> or long double data[nx*ny] instead. Then you can easily write or read by:

fwrite(data, nx * ny * sizeof(long double), 1, file);
...
fread(data_read, nx * ny * sizeof(long double), 1, file );
4
  • @WhozCraig So if I understand correctly, I must write into the file row by row?
    – erthalion
    Jan 19, 2014 at 5:37
  • @erthalion Given your implementation, yes. Personally I'd use the C++ standard library rather than writing C-code in a C++ program, but that choice is yours.
    – WhozCraig
    Jan 19, 2014 at 5:40
  • @WhozCraig I just noticed OP's data is not continuous. Thanks. But still, OP also need to fix these stuff in order to get correct result. Jan 19, 2014 at 5:43
  • @erthalion Updated and fix other stuff. Jan 19, 2014 at 5:53

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.