Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Objects Saving themselves to files

This topic is 6944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Look here at this code. This is my file header for my level files for BeanWars, my RTS game I''m making. This is my declaration of my structs/classes and the save function. This doesn''t work: struct AISettings { bool IsBuilding; int AttackDelay; int AttackInterval; int AttackPercent; int AIDelay; }; class LevelFileHeader { public: LPSTR Title; int NumCreatures[8]; int NumFutureCreatures[8]; int Mapxs; int Mapys; int MapX; int MapY; AISettings AI[8]; void Save(ofstream &fout); void Load(ifstream &fin); }; void LevelFileHeader::Save(ofstream &fout) { fout<<(char)StrLen(Title); for(int i = 0; i < StrLen(Title); i++) fout<; for(i = 0; i < 8; i++) fout.write((char*)NumCreatures[0], sizeof(NumCreatures[0])); for(i = 0; i < 8; i++) fout.write((char*)NumFutureCreatures[0], sizeof(NumFutureCreatures[0])); fout.write((char*)Mapxs, sizeof(Mapxs)); fout.write((char*)Mapys, sizeof(Mapys)); fout.write((char*)MapX, sizeof(MapX)); fout.write((char*)MapY, sizeof(MapY)); for(i = 0; i < 8; i++) { fout.write((char*)AI[i].AIDelay, sizeof(AI[i].AIDelay)); fout.write((char*)AI[i].IsBuilding, sizeof(AI[i].IsBuilding)); fout.write((char*)AI[i].AttackDelay, sizeof(AI[i].AttackDelay)); fout.write((char*)AI[i].AttackInterval, sizeof(AI[i].AttackInterval)); fout.write((char*)AI[i].AttackPercent, sizeof(AI[i].AttackPercent)); } return; } ofstream savefile("level1.bwl"); Header.Save(savefile); Why doesn''t this work? Any suggestions on how to do this easier? BTW, savefile.write doesn''t work. You can''t convert this data type to char*. ~BenDilts( void );

Share this post

Link to post
Share on other sites
Firstly, you''re trying to do binary output to a file opened in textmode. ofstream savefile("level1.bwl") opens a file for writing in text mode by default, unless you specify ios_base::binary as extra parameter.
If you write to an ofstream in binary mode, only ofstream::write will work. << or >> won''t work with binary files.

Secondly, ofstream::write expects a pointer to the first byte of the data you''re writing. So, you''d have to use (char *)&Mapxs instead of (char *)Mapxs.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!