Beginner losing his mind trying to understand serialisation in c++, help PLEASE!

This is a discussion on Beginner losing his mind trying to understand serialisation in c++, help PLEASE! within the C++ Programming forums, part of the General Programming Boards category; Hello all. Okay i'll try to get straight to the point here and not waste anyones time. I'm a beginner ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Beginner losing his mind trying to understand serialisation in c++, help PLEASE!

    Hello all.

    Okay i'll try to get straight to the point here and not waste anyones time.

    I'm a beginner when it comes to programming, my introduction to the world of programming was with python. About a week ago i decided to start learning c++ as well, as it seems to me learning more than one language would make me a better programmer overall. Now i've been progressing okay with c++, that is up until now.

    I want to acheive something similar to pickling and unpickling in python. For example lets say i have a simple class like:
    Code:
    class Journal{
    
    private:
    int ID_number;
    string text_entry;
    
    public:
    Journal(int x, string y){
    ID_number = x;
    text_entry = y;
    }
    
    void display_data(void){
    cout << "\nID: " << ID_number << "\nText: " << text_entry << endl;
    };
    So obviously an instance of this class would be declared like Journal test(1, "text");

    Now all i want to be able to do is store this class instance and it's data into a file in binary form so that i can save the data upon the program being closed, and then retrieve it another time for future use.

    Exactly like using the pickle module in python. Now i cannot for the bloody life of me understand why this is not easier to do in c++. What is the standard way people save their data with c++?

    Basically i just want to be able to store an object in a file so it is saved to the hard drive, then be able to read it another time and use the data again in future. Exactly like pickling and unpickling objects in python.

    This problem has bloody stumped me, and before you guys flame me i definitely have put effort into searching for my own answers but have not been able to work this out on my own.

    Would someone here please be able to help me understand how to acheive what i assumed would be a trivial thing to do in c++ but that does not seem to be the case at all, although i'm sure it's not difficult once you understand how to go about acheiving this.

    This problem has pretty much created a road block in my c++ learning because i cannot create any real useful programs if i cant even save data and retrieve it another time.

    I would be incredibly grateful if someone could spend the time to help me understand and acheive this operation.

    I'm starting to feel disillusioned, how can something so trivial and simple in python be so bloody hard to fathom in c++.

    I understand everyone has their own projects and work to do so any time spent helping me and my problem here would be *hugely* appreciated and you would not be wasting your time i assure you. I'm beginning to feel quite desperate and starting to despair.

    Thankyou

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Python's pickle does serialization. There is no standard library support for serialization in C++, but third party librarues are available, e.g., Boost.Serialization.

    However, seeing that you are dealing with journal entries, a better long term solution is to use a database engine. For example, if you use SQLite, you can simply save each Journal object as a row in a database (which for SQLite is just a file).

    That said, you could always design your own file format, then write to a file. How this is done depends on what exactly are your requirements, e.g., are you willing to impose a maximum length for a journal entry, or store the entry length, or designate a special character, etc.
    Last edited by laserlight; 03-15-2012 at 01:08 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Quote Originally Posted by laserlight View Post
    Python's pickle does serialization. There is no standard library support for serialization in C++, but third party librarues are available, e.g., Boost.Serialization.

    However, seeing that you are dealing with journal entries, a better long term solution is to use a database engine. For example, if you use SQLite, you can simply save each Journal object as a row in a database (which for SQLite is just a file).

    That said, you could always design your own file format, then write to a file. How this is done depends on what exactly are your requirements, e.g., are you willing to impose a maximum length for a journal entry, or store the entry length, or designate a special character, etc.
    I like the look of the boost libraries they seem to be exactly what i'm looking for. I'm just abit confused though if i need to download some third party files or if i can just throw:
    Code:
    #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp>
    into my file and then use the boost libraries. ........ is just so confusing to me.

    Sorry to asking you to spoon feed me information but would you be able to elaborate abit on how i would go about using the boost libraries.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by blayze101
    I like the look of the boost libraries they seem to be exactly what i'm looking for. I'm just abit confused though if i need to download some third party files
    You need to download the files. Unfortunately, Boost.Serialization is not a header-only library, so you also have to compile and link. You'll need to start from the Boost getting started page. A complete build will take a great deal of time, so check out the options to build only what you want. (Of course, also check out the other libraries available that require building: stuff like Boost.Filesystem and Boost.ProgramOptions could also be useful to you.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    It's up to you.

    Serialisation is about storing objects (or, states of objects) to some form of persistent storage, in a manner that they can be retrieved.

    There are many ways of doing that. The "right" one depends on your requirements. You might do it using a text file on disk, using a database (distributed or not), in some portable binary format, in some compiler-specific binary format, in some compressed format, using XML, etc etc.

    If you're not sure, I suggest keeping it simple. For example, writing to a text file (which means you need to write code both to write objects out, and retrieve them). If you find that isn't good enough for some reason, ask yourself why: that way you can identify particular requirements you have related to serialisation. As your requirements evolve, you might change serialisation technique, select a particular third-party library, etc etc.

    If you want to use boost, it's not a free lunch. There is effort to understand, use, and evaluate. Just like any other method.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,153
    google protocol buffers work pretty well for serialization, and they are supported by MANY languages and platforms (java, C++, and python are the officially supported languages). they serialize to a portable, compact binary format, and work well for saving to a file or transmitting over the network.

  7. #7
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    boost & gpb (and many others, its hardly a new problem and there are lots of solutions out there) are great but if you want to learn, roll your own custom solution for your app, its the best way to learn, especialy if your data classes are a hetrogenous collection. Use templates to unroll the classes and then write the compenent members out with a ostream derived class, its not too difficult and you'll really learn alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  2. XML Serialisation
    By bookworm in forum C# Programming
    Replies: 1
    Last Post: 07-30-2003, 07:05 AM
  3. losing weight
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 10-21-2002, 05:43 AM
  4. Don't know if I'm losing my mind...
    By Invincible in forum C++ Programming
    Replies: 19
    Last Post: 05-26-2002, 11:27 PM
  5. Lost source... losing mind...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-27-2001, 12:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21