1
std::vector<std::vector<std::vector<sphere_data> >> Sphere_image ; 

I want to write the data in sphere image to binary file.

Could any one tell me how can this be done.

I tried this Code:

ofstream fout("C:/Project/data.dat", ios::out | ios::binary);
fout.write((char*)&Sphere_image[o], Sphere_image.size() *     
sizeof(sphere_data));
fout.close();
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Seems your data structure has become spherical itself. You can try using three nested loops to get the nested vector contents and write to file. – seccpur Oct 05 '16 at 11:21
  • Why the downvote for the question - he's tried to do something and has posted the problem very clearly? – UKMonkey Oct 05 '16 at 11:23

2 Answers2

2

A multi-dimensional vector is not stored in consecutive memory locations, like a multi-dimensional array is.

Your vector contains a

 std::vector<std::vector<sphere_data>>

This is just an array of vector structures themselves. Sphere_image.size() gives you the number of values in the top level dimension of the multi-dimensional vector, that's all.

First of all, this will only work if sphere_data is a POD. If it is a class, this will not work. You have to iterate over each dimension separately:

ofstream fout("C:/Project/data.dat", ios::out | ios::binary);

for (const auto &dim: Sphere_image)
   for (const auto &dim2:dim)
      fout.write(&dim2[0], dim2.size() * sizeof(sphere_data));

fout.close();
Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • this code gives me error at &dim2[0]. The error is : argument of type "cons sphere_dat*" is incompatible with parameter of type const char". – user6038334 Oct 05 '16 at 11:25
  • 1
    @user6038334: The first argument needs to be `reinterpret_cast(&dim2[0])`. That `reinterpret_cast` is a huge red flag - specifically, that you are assuming sphere_data is POD. – Martin Bonner supports Monica Oct 05 '16 at 11:46
2

When you have nested std::vectors, you don't have contiguity in memory for the whole data structure.

So, you have to iterate through all the nested vectors "dimensions", and assume contiguity of sphere_data instances only in the innermost vector.

So, your line:

fout.write((char*)&Sphere_image[o], Sphere_image.size() *     
sizeof(sphere_data));

must be expanded something like that:

for (const auto& vi : Sphere_image) { // For each outer vector
  for (const auto& vj : vi) {         // For each sub-vector
    // Now you do have contiguity for vj
    fout.write(
        reinterpret_cast<const char*>(vj.data()), 
        vj.size() * sizeof(sphere_data));
  }
}

Note that this assumes that sphere_data is a POD, so e.g. if you have pointer data members inside sphere_data, that won't work.

In this case, you may provide a sphere_data::save(std::ofstream& out) const method, which you can call in the innermost loop. Instances of sphere_data will know how to serialize themselves to binary files. E.g.:

for (const auto& vi : Sphere_image) { // For each outer vector
  for (const auto& vj : vi) {         // For each sub-vector
    for (const auto& sd : vj) {       // For each sphere data
      sd.save(fout);
    }  
  }
}

You may also provide a symmetrical sphere_data::load() method as well.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Thanks a lot this helped. Also while reading this binary file do I need to do the same way? only with ifstream? – user6038334 Oct 05 '16 at 11:56
  • Now here is a problem while reading the same. Could you please help – user6038334 Oct 05 '16 at 12:15
  • When reading data from file, you have to build and populate the vectors, so the code is different. I don't know your problem in details, but for example you may need to write on your files the number of vector items for each dimension, and read those numbers back and create vectors of proper sizes and then populate them with sphere_data read from files. – Mr.C64 Oct 05 '16 at 13:04
  • Yes I was trying to do: struct sphere_data { int Camno; int cam_x; int cam_y; }; std::vector s_pixel; std::vector> s_img; std::vector >> Sphere_image ; Now I have written the data from sphere_image to binary file now further I want to read the binary file so that I can access the data – user6038334 Oct 05 '16 at 13:20
  • You may want to mark the best reply as answer, up vote all the helpful replies, and then write another specific question for your problem. – Mr.C64 Oct 05 '16 at 13:30