473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Serializing A Struct

Hello all,

I'm attempting to serialize the following:

Expand|Select|Wrap|Line Numbers
  1. typedef struct 
  2. {
  3.     const char* name;
  4.     const char* layer1[12];
  5.     const char* layer2[12];
  6.     const char* layer3[12];
  7.     const char* layer4[12];
  8.     bool passability[12*800][800];
  9. }DataMap;
  10.  
  11.  
  12. typedef struct 
  13. {
  14.     DataMap map;
  15. }MapManager;
  16.  
And my code for serializing this is:
Expand|Select|Wrap|Line Numbers
  1.          // Save
  2. private: System::Void SaveMap(MapManager* map)
  3.          {
  4.              ofstream ofs(StringToChar(CharToString(map->map.name) + ".map"), ios::binary);
  5.              ofs.write((char *)&map->map, sizeof(map->map));
  6.          }
  7.          // Load
  8. private: System::Void LoadMap(System::String^ fname, MapManager* map)
  9.          {
  10.              ifstream ifs(StringToChar(fname), ios::binary);
  11.              ifs.read((char *)&map->map, sizeof(map->map));
  12.          }
  13.  
(This is in .Net but I need to serialize in Cpp cause the game is in non-managed Cpp. You can ignore the StringToChar and CharToString.)

However, I recently learned that I can't serialize pointers which puts me in a bind because I don't know any other way to go about this.

What I'm trying to do is make a simple map editor that saves map data and loads it. I also want to be able to load this data from the game I'm programming. So I decided to use a simple struct the hold the data.

note: I don't necessarily need to use "const char*" if there is a way to convert an object to "const char*" but like i said, I don't know any other way to go about this. The reason I'm using "const char*" is because the engine i'm using only accepts "const char*" for loading textures and so on.

What should I do to save/load my data? Is there a workaround I can do?
I don't necessarily want to know a way to serialize pointers themselves because it might become troublesome in the future, so a workaround would be better. Such as getting the data that the pointer is pointing to a temporary variable and saving that variable instead and loading it in reverse.

(If you need more info than just ask :))
Oct 17 '08 #1
1 3938
weaknessforcats
9,208 Expert Mod 8TB
That's true, you cannot serialize pointers. However, you can serialize offsets. So determine the offset from the start of the array to the char*.

Next, write the data pointed at vt the char*. Precede this data with the length oif the data and precede that by the offset.

On the readback, you create a array of char*. Then start reading. The offset will tell you whuich array element to use, the length ofthe data will tell you how many bytes of memory to allocate for the data, and then you copy the data from the disc to this new allocation.

I recommend you use a Serialize object designed as a Visitor. You would visit the datamap object with the Serialize object and you data goes to disc. That is, no code in the datamap object about serialization. Similary, a Restore object can visit an empty datamao object and pump if full of data.

Check out the Visitor design pattern: http://bytes.com/forum/thread674645.html.
Nov 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
5
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
1
by: Chris | last post by:
I'm having trouble Serializing a System.Data.DataColumn object. When I try to serialize it, I get the following: System.NotSupportedException: Cannot serialize member...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
0
by: Olav Langeland | last post by:
I have a structure like this: public struct MyStruct { public long lVal; public short sVal; } The structure is used like this:
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
2
by: (2b|!2b)==? | last post by:
I have a struct declared as follows: struct RecordType1 { unsigned int dt : 24; //3 bytes unsigned int ts : 16; //2 bytes unsigned int lsp : 24; //3 bytes (float value represented as int)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.