It's probably obvious that I'm more familiar with Python than C++...
I was wondering if there's a convenient way of storing objects / miscellaneous STL structures on the hard drive for later importing and use in C++ in the same manner that Python does with the Pickle module.
I know I can write a raw dump, but I'd rather avoid that. I'm also working with some god-awfully huge files, so some compression would also be a bonus.

A bit of Googling led to the conclusion that I don't know how to word this question in a way that a search engine can understand.

Thanks in advance!

Recommended Answers

All 3 Replies

Member Avatar

This sounds like serialisation? Boost sounds like a good option.

I help maintain a C++ library called the PicklingTools: One of its main goals is to allow C++ users to pickle data so that Python can read it.

The library gives you data structures that "look like" Python from C++, and then allows you to pickle/unpickle those. All (?) the basic types are provided: int, float, long int, string, None, dict, list, ordered dict, tuple. There are some hooks to use user-defined types, but those are a bit immature at the moment.

Here's a simple C++ example:

#include "chooseser.h"

int main()
// Here's a complicated Python-esque dictionary
Val v = Tab("{ 'a':1, 'b':[1, 2.2, 'three'], 'c':None }");
cout << v["a"] << endl; // prints 1

// Write the dictionary to Disk AS A PICKLE
DumpValToFile(v, "example.p0", SERIALIZE_P0); // can also be SERIALIZE_P2

// From Python, you can pickle.load(file('example.p0'))

// Read it back in
Val result;
LoadValFromFile("example.p0", result; SERIALIZE_P0);
cout << result << endl; // print

The library has been around for a while (8 years?) and is active.
There is also a User's Guide and FAQ in html and pdf and text in the documentation

Hope you find this useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.