Reading and Writing Boost multi_array's to binary files

Posted on 2004-08-20
Last Modified: 2010-08-05
I am having problems writing a boost multi_array to a binary file and then reading it back.

I have a 3d multi_array which contains integers:

typedef boost::multi_array<int, 3> i3dArray;
typedef i3dArray::index iIndex;

Start Program
   i3dArray contactMatrix(boost::extents[NOATOMS][NOATOMS][NO_DISTANCES_BINS]);  // [167][167][150]

   loop through files of atomic structures
        count the number of atom types i see at different distances and increment the relevant array positions

      // Verify values - this works (i.e. the i3dArray has correct values)
     for(iIndex i = 0; i != 50; ++i)
        for(iIndex j = 0; j != 50; ++j)
           for(iIndex k = 0; k != 50; ++k)
              cout << contact[i][j][k];

     // Then i want to write this object to a binary file.
    ofstream outfile( outname.c_str(), ios::out | ios::binary);
      outfile.write( (char*)&contact, sizeof(contact) );

    // later i want to read in this object
    ifstream Source(file, ios::in | ios::binary );
    if (
      exit(1); (char*)&contact, sizeof(contact) );

      // Printing gives me a Segmentation Fault
     for(iIndex i = 0; i != 50; ++i)
        for(iIndex j = 0; j != 50; ++j)
           for(iIndex k = 0; k != 50; ++k)
              cout << contact[i][j][k];

I know this is a crude and incompetent method of writing an object but more correct ideas would be appreciated.
Question by:keepthursdayspecial
  • 2
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 11851593
You need to access the multi_array's data area by calling the 'data()' meber function, i.e.

   ofstream outfile( outname.c_str(), ios::out | ios::binary);
     outfile.write( (char*), sizeof(int) * contacts.size());

   ifstream Source(file, ios::in | ios::binary ); (char*), sizeof(int) * contacts.size() );

Just taking the multi_array's address will not work, since this is not just 'flat' data...


Author Comment

ID: 11852059
Oh, I see.

However, this doesn't write anything to the binary file.

Author Comment

ID: 11852231
     string outname = "out/pdb"+pdbfiles[t]+".ent.chem";
      ofstream outfile( outname.c_str(), ios::out | ios::binary );
      outfile.write( (char*), sizeof(int)*contactMat.size() );

when i check the sizeof(int)*matrix i get 72 which is the same size as the output file.  

      cout << sizeof(int) << " " << sizeof(int) * contactMat.size() << " " << contactMat.size() << endl;

So 4 72 8.

The multi_array is [18] [18] [150] is this instance.

When I read the file back into a new multi_array of the same size i get an array of 0's.

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to fix  a C++ segmentation violation which occurs in a static member function? 22 108
C++ assignment question 7 199
C++ question 3 65
Exception thrown at 0x00007FFD5BC81F28 7 44
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question