2

I need to write a matrix with four columns ("g_Grid.r", "g_Grid.t", "g_Grid.b", "g_Grid.ph")

Normally, I write to file using file stream:

ofstream fout; 
fout.open("GRID.dat");
 for (int m=0;m<N_PH;++m)
    {   
    for (int k=0;k<N_B;++k)
        {
          for (int j=0;j<N_T;++j)
            {   
            for (int i=0;i<N_R;++i)
                {
                  fout << setprecision(32) << g_Grid.r[i]<<" "<<g_Grid.t[j]<<" "<<g_Grid.b[k]<<" "<<g_Grid.ph[m]<< endl;

                }
            }
        }
    }
 fout.close();

It works fine, but now I'm dealing very large (long) matrix and it takes ages to write the formatted output (".txt"). Since I really do NOT need the file to be formatted, it prefer to write it as a binary file.

QUESTION: given four vectors/arrays (r,t,b,ph), how to write a table to binary file? (I use matlab to read this file later, and also need to read it as a binary table)

EDIT

Normally, I use the following simple code to write binary file:

ofstream myFile (fileName, ios::out | ios::binary);
double val;
for (int m=0;m<N_PH;++m)
    {   
    for (int k=0;k<N_B;++k)
        {
          for (int j=0;j<N_T;++j)
            {   
            for (int i=0;i<N_R;++i)
                {
                  val = g_N.Amp[m][k][j][i];
                  myFile.write(reinterpret_cast<const char*>(&val), sizeof(val));
                }
            }
        }
    }
myFile.close();

But this one will yield only "one-column" while it will be read by matlab later.

halfer
  • 19,824
  • 17
  • 99
  • 186
Arnold Klein
  • 413
  • 2
  • 6
  • 16
  • Does matlab just read a plain binary file with floating point numbers and no "extra" information? – Mats Petersson Jun 12 '13 at 08:42
  • @Mats Petersson What do you mean? I use very simple commands to read the binary file: fid = fopen("name.dat"); X=fread(fid,'double'); Basically, I can rearrange the "four-column" table as "one-column", it will be read by matlab and to restore the "four-column" later. It is doable. I thought about simpler way to write and read the table as is, without doing additional "black-magic". – Arnold Klein Jun 12 '13 at 08:48
  • Ok, don't know much about Matlab, so didn't know you could simply tell it to read a bunch of float from a file. Will write an answer... – Mats Petersson Jun 12 '13 at 08:49
  • Following the same logic, as in the case of formatted output (with ofstream): "Stream the entire table to a file, and read the entire table". I'm sure, one it is written as a table, matlab will be able to read a table from binary file (probably it reads by lines or so) – Arnold Klein Jun 12 '13 at 08:51
  • Yeah, you're right! Matlab reads "lines and converts to doubles" – Arnold Klein Jun 12 '13 at 08:51

1 Answers1

5

To write binary data into a file, you first need to open the file in binary mode, you need to use fstream::write. It takes a char * argument, and writes a number of bytes from that - the char * argument requires a cast if the data is not an array of char or pointer to char.

So as to avoid having several calls to write, it's better to stuff the data into a structure.

So, the following should do what you need:

ofstream fout; 
fout.open("GRID.dat", ios::binary);
 for (int m=0;m<N_PH;++m)
    {   
    for (int k=0;k<N_B;++k)
        {
          for (int j=0;j<N_T;++j)
            {   
            for (int i=0;i<N_R;++i)
                {
                  struct X
                  {
                      double a, b, c, d;
                  } x;

                  x.a = g_Grid.r[i];
                  x.b = g_Grid.t[j];
                  x.c = g_Grid.b[k];
                  x.d = g_Grid.ph[m];
                  fout.write(reinterpret_cast<char *>(&x), sizeof(x));
                }
            }
        }
    }
 fout.close();
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Could you please show, how to READ this binary file with C++ and STORE again in r,t,b,ph? (I use both, matlab and C to work with files). (I use IO as intermediate step in my program. So I need to write to a file and to read again) – Arnold Klein Jun 12 '13 at 09:05
  • Same principle, just use `ifstream` instead of `ofstream`, and `read` instead of write. [probably a good idea to change the name of `fout` to `fin`, or some such too, but functionally, that's not needed] – Mats Petersson Jun 12 '13 at 09:09
  • I understand. It means, that each fifth element corresponds to the same "column", Well, I need to play with this to get it work properly. Thanks!!! – Arnold Klein Jun 12 '13 at 09:14