[General Q] Saving and loading a struct

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

[General Q] Saving and loading a struct

Post by Bate »

Is that going to work properly in all cases? I'm not quite sure if there might be a problem with casting strings.

Code: Select all

struct SData
{
  std::string a[100];
  std::string b[100][100];
  int c[100];
  float d[100][100];
};

std::string filepath = "test.dat";

void CSaveEngine::saveData(SData data)
{
  std::fstream binFile(filePath.c_str(), std::ios::binary | std::ios::out);
  binFile.write( reinterpret_cast<char*>(&data), sizeof(SData) );
  binFile.close();
}

SData CSaveEngine::loadData()
{
  SData data;

  std::fstream binFile(filePath.c_str(), std::ios::binary | std::ios::in);
  binFile.read( reinterpret_cast<char*>(&data), sizeof(SData) );
  binFile.close();

  return data;
}
Never take advice from someone who likes to give advice, so take my advice and don't take it.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

That code is not safe at all. Using read and write that way is only safe for plain-old-data types. std::string is not a plain-old-data type.

Travis
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

So I need multi-dimensional char arrays, right? It works with arrays, though, doesn't it? Or is there a better solution?

Thanks
Never take advice from someone who likes to give advice, so take my advice and don't take it.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The better solution would be to write proper code to serialize your object type. It would also be a good idea to use a vector or list instead of fixed length arrays.
Josie
Posts: 44
Joined: Sat Jul 25, 2009 4:08 am
Location: Griffin, Georgia, US
Contact:

Post by Josie »

Vitek is right, however vague.

Two good places to start with serialization are the wikipedia article and C++ faq lite.

Boost has an excellent serialization library.
Bate
Posts: 364
Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany

Post by Bate »

Thanks, Boost's serialization looks promising.
Never take advice from someone who likes to give advice, so take my advice and don't take it.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

vitek wrote:That code is not safe at all. Using read and write that way is only safe for plain-old-data types. std::string is not a plain-old-data type.

Travis
Thats just wrong! all std types can be serialized directly like this! works seamles. problem arrises when u start using irrlicht types and pointers which don't serialize at all.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply