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.
Bate
Posts: 364 Joined: Sun Nov 01, 2009 11:39 pm
Location: Germany
Post
by Bate » Tue Jun 08, 2010 6:16 pm
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 » Tue Jun 08, 2010 8:59 pm
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 » Tue Jun 08, 2010 9:04 pm
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 » Wed Jun 09, 2010 4:19 pm
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 » Thu Jun 10, 2010 1:13 am
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 » Thu Jun 10, 2010 1:44 pm
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 » Thu Jun 10, 2010 8:48 pm
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.