You cannot record objects and then read them to memory. You need to serialize them.
In fact you’re trying to make a serialization when you use the cast (char *)
but it doesn’t give the result you expect.
In general you should save this data as text. Not that it cannot be binary, but you will have to create a format for this binary. It is not only throw any information there. In the text also needs a format but there is easier because it has some obvious and simple to manipulate.
Just because a file is considered binary does not mean that you can play binary code or direct information from memory. This binary means that the data there should be processed byte to byte and not consider there is anything specific in it. It should be treated as a sequence of bytes and the program must know how to treat them.
In a text file there are some assumptions such as the existence of line break.
As C++ does not have its own functionality in its library that facilitates serialization. There are third-party libraries for this.
The first recommendation I make is to write in text file. Unless you really need it to be binary and you want to afford the complexity of doing this.
The second is to prepare a form if serialization to record and deserialization after reading.
The first will pick up each of the members of the type DateAdress
and will transform it into a sequential text that can be written to the file. Note that it may be necessary to make some conversions into numeric types and especially into types that are actually pointers. You cannot record the pointer, you will need to record the data that the pointer points to (this includes a recursive process) or some identification so that later an information is associated with another correctly (this is not easy to do correctly).
When reading this serialized data in the file you will have to do the reverse process, interpret each information in the text according to its size or through special markers (if it keeps the order of the fields), or that knows identifiers for each field (then you don’t need to keep order, an example of this is the JSON format). Picking up each information individually can put on the appropriate member of the type DateAdress
, making the appropriate conversions.
Is it complicated? Yeah. Is there a better way? I don’t know.
I just want to stress that eventually a binary file may be more useful, the big difference is that you will have to understand the content without any help from the standard adopted in texts.
The error occurs because deep down you are throwing dirt in memory.
The cast that you used is not recommended in C++. I’m not saying it will work but it would have helped if you had done reinterpret_cast<const char*>(&writer)
. I have no way to test here but I don’t know if the same would apply to reader
. That’s just an idea.
I’m not saying what you’re doing can’t work, just that it’s a beautiful gambit. I’ll even suggest a change that might solve your situation if the guy DateAdress
is simple enough. Note that it can work but is a fragile solution.
Read the data individually instead of reading the whole structure:
ObjectReader.read((char *)&reader.campo1, sizeof(reader.campo1));
ObjectReader.read((char *)&reader.campo2, sizeof(reader.campo2));
ObjectReader.read((char *)&reader.campo3, sizeof(reader.campo3));
Since I don’t know the composition of the guy, I kicked three names into the fields.
But also to work it might be necessary to "compact" the structure, which is not always desirable. I never did but I know that you need to use a #pragma
:
GCC:
struct __attribute__((packed)) nome { ... };
Microsoft:
#pragma pack(push, 1)
struct nome { ... };
#pragma pack(pop)
The problem is not so simple and you did not give accurate information, so I gave an answer that may help to find the way but I do not guarantee that it is the definitive solution.
– Maniero