archive write issue

I'm trying to write a array into a binary archive but appears 2 weird errors.
The array to be written is pointer array, like a dynamically allocated matrix, does it changes the write process?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;

void Hash::store_array(char name){
    
    ofstream store(name,ios::binary, ios::in, ios::app);
    
    while (not store) {
	cerr << "Problem to open the archive, please type the name again : ";
	char new_name[20];
	cin >> new_name;
	
	ofstream store(new_name,ios::binary, ios::in, ios::app);
	cout << endl;
    }
    
    store.write(reinterpret_cast<char*>(&elementos), (sizeof(elementos)));
}

it does not matter what the data is or where it came from.

What are the errors?

I suspect you messed up sizeof (this argument to write is total # of bytes to write, so if you had
int x[100] then you write (blah, sizeof(int)*100).

You have to be careful writing objects. If the object contains a pointer, explicit or understood, you have to unravel that so it writes the DATA in the pointer not the POINTER VALUE. For basic types, the above is all you need, but for objects, you usually need to write a function that collapses it to pure bytes that you can write. This includes stl containers, you can't just write a class that has vectors inside, the vector data isnt visible to write directly!





Last edited on
Topic archived. No new replies allowed.