0

I want to use pointer magikry to save a C++ class using the following method that writes byte data into a file:

result Osp::Io::File::Write (const void *buffer, int length);
  • Parameters:

    • buffer — A pointer to the user-supplied buffer that contains byte data to be written
    • length — The buffer length in bytes
  • Exceptions:

    • E_SUCCESS — The method is successful.
    • E_INVALID_STATE — The file has not been opened as yet.
    • E_ILLEGAL_ACCESS — The file is not opened for write operation, or access is denied due to insufficient permission.
    • E_INVALID_ARG — Either of the following conditions has occurred:
      • The specified buffer contains a null pointer.
      • The specified buffer length is equal or smaller than 0.
      • The file handle is invalid (either the file is closed by another method, or the memory is corrupted).
    • E_STORAGE_FULL — The disk space is full.
    • E_IO — An unexpected device failure has occurred as the media ejected suddenly or file corruption is detected.

I'd rather not assume that there will be any sort of buffering, although I am confident each byte won't occasion a whole block of flash to be rewritten but I was wondering if there is a niftier way to write all the data fields of a class (and nothing else, eg static fields) by, eg, a pointer to the object (*this)?

ruakh
  • 175,680
  • 26
  • 273
  • 307
John
  • 6,433
  • 7
  • 47
  • 82
  • So you're asking if C++ has any built-in serialization mechanisms for classes and structs? – Cody Gray - on strike Jan 23 '12 at 23:28
  • I don't think C++ >> stream operators are available to me. @Cody, yes, as long as you don't mean << and >> – John Jan 23 '12 at 23:28
  • Most C++ structs cannot be serialized bitwise, you'll have to use streams or similar. – Mooing Duck Jan 23 '12 at 23:34
  • Forgive me if I'm missing the question completely, but couldn't you just do Osp::Io::File::Write(&SomeObj, sizeof(SomeObj))? Hopefully you're just serializing POD types. – Jon G. Jan 23 '12 at 23:36
  • @JonG. Yup, that's what I thought, but now I remember leaving a `Osp::Base::String` in there too. – John Jan 23 '12 at 23:43

1 Answers1

2

In C++ you don't write "raw" objects into files, but rather serialize them. There's no magic, you need to write your serialization code yourself (overloading operators << and >>, for convenience).

You can do it the old C-style by just dumping memory, but in addition to the problems this would generally cause with C (alignment, endian issues when transferring data between systems), you also get the problems introduced by C++ (internal class representation, possible "hidden" data members such as a v-table, etc).

If you want to ensure you read and write reliable data that can be transferred between different systems and/or different pieces of software - you better implement the serialization, and don't look for shortcuts.

You can use libraries like Boost.Serialization for that.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
littleadv
  • 20,100
  • 2
  • 36
  • 50
  • OK, I found a non POD member that would have mucked up my plan anyhoo. – John Jan 23 '12 at 23:38
  • 1
    Writing raw objects into files is nothing you "don't" do in C++, for PODs it's often the most effective solution. For many other types of data it's not, but the standard stream operators are also not good for all kind of data. – leftaroundabout Jan 23 '12 at 23:57
  • @leftaroundabout +1 agree 100% with your statement - PODs are great for maximizing efficiency in C++, block writes, copies, etc... – kfmfe04 Jan 31 '12 at 03:14