我使用C++生成一些我想在Python程序中訪問的數據。我已經想出瞭如何使用C++提升序列化/反序列化二進制文件,但不知道如何在Python中訪問數據(不需要手動解析二進制文件)。C++中的boost :: serialization,Python中的反序列化
這裏是系列化我的C++代碼:
/* Save some data to binary file */
template <typename T>
int serializeToBinaryFile(const char* filename, const T& someValue,
const vector<T>& someVector)
{
ofstream file(filename, ios::out | ios::binary | ios::trunc);
if (file.is_open())
{
boost::archive::text_oarchive oa(file);
int sizeOfDataType = sizeof(T);
oa & sizeOfDataType;
oa & someValue;
oa & someVector;
file.close();
return 0;
} else {
return 1;
}
}
這裏是我的下反序列化++代碼:
/* Load some data from binary file */
template <typename T>
int deSerializeFromBinaryFile(const char* filename, int& sizeOfDataType,
T& someValue, vector<T>& someVector)
{
ifstream file(filename, ios::in | ios::binary);
if (file.is_open())
{
boost::archive::text_iarchive ia(file);
ia & sizeOfDataType;
ia & someValue;
ia & someVector;
file.close();
return 0;
} else {
return 1;
}
}
我如何加載一個Python程序的價值和矢量對象?
你可以使用boost :: python包裝你的反序列化代碼並調用它。 – m0nhawk
恕我直言,更好的方法是使用[Google protobuf](https://developers.google.com/protocol-buffers/docs/overview) – megabyte1024