Solved

Reading and Writing Boost multi_array's to binary files

Posted on 2004-08-20
3
471 Views
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
         i3dArray[i][j][k]++
    }

      // 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) );
    outfile.close();    

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

      // 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.
0
Comment
Question by:keepthursdayspecial
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
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*)contact.data(), sizeof(int) * contacts.size());
   outfile.close();    

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

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


0
 

Author Comment

by:keepthursdayspecial
ID: 11852059
Oh, I see.

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

Author Comment

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

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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now