Jump to content
  • Advertisement
Sign in to follow this  
Plethora

File i/o troubles... losing strings??

This topic is 3720 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

Ok so I spent a few hours creating a little backend monster-creator utility for myself but I have a bug in it that I haven't quite figured out. The basic idea is to just have a very simple console application which uses the Monster class from my main game (and only the monster class) to create the various monsters I am going to need and saves them to a file. I'm saving them as binary data (which I have done before), but I am getting a wierd error which based on my understanding of how streams and file i/o work, shouldn't be happening. Which either means I have a faulty understanding OR something in my code isn't doing what I think its doing. Anyway, here is the relevent code:

        else if (menuChoice == 4)   //Create New
        {
            mon1 = createNewCreature();
            monsterList.push_back(mon1);
        }
        else if (menuChoice == 5)   //Save
        {
            ofstream fout("CreatureList.dat", ios_base::binary);
            for (int i = 0; i < monsterList.size(); i++)
            {
                fout.write((char*)(&(monsterList)), sizeof monsterList);
            }
            fout.close();
        }
        else if (menuChoice == 6)   //Load
        {
            monsterList.clear();
            Monster temp;
            ifstream fin("CreatureList.dat", ios_base::binary);
            while(fin.read((char*)(&temp), sizeof temp))
            {
                monsterList.push_back(temp);
            }
            fin.close();

            currentKey = monsterList.size();
        }


monsterList is a vector and createNewCreature creates and returns a new creature to add to the list. Now the bug is this. I can create creatures and add them to the vector just fine. I display them and as far as I can see, everything is peachy. The problem occurs either with saving or loading the list (I can't tell which offhand because if its in the saving then loading will obviously just be loading faulty data). All the integer data members of the monster class seem to be saved and loaded just fine. After I save and load I get the correct integers without a problem. But the Monster class also has a string in it called name. I cannot get the creatures name to save/load correctly. Now what has been bugging me is that I rather thought that when saving binary data that an object was kind of a complete entity, so to speak. When you save it to a file its either saved correctly or not. I don't really see how it is possible for individual data members to be saved correctly while others aren't. If my understanding on this is wrong please correct me. Anyway, I can give further information if needed, or maybe take a screenshot of a typical run-through if that would help.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Plethora
I save and load I get the correct integers without a problem. But the Monster class also has a string in it called name. I cannot get the creatures name to save/load correctly.

Strings are little more than a pointer to an array of chars.
The rule is never, ever, ever write a pointer to a file - in this case that's all you're doing, writing a pointer to a file, not the contents of the string.

The way to do this right is to read/write individual data members in whatever way they're intended to be interpreted.
Generally speaking binary files aren't portable anyway but text files are.

You could read/write all data members in text mode using the stream operators (<< and >>), if your string is a std::string (as you should be [smile]) then it'll work just fine.
To avoid breaking encapsulation by accessing those individual data members you can overload the stream operators for the Monster class and make them friend functions, reading and writing a monster would then be as simple as:

fin >> someMonster; // Load a monster
fout << someMonster; // Save a monster

Share this post


Link to post
Share on other sites
Ya know I had text files that worked perfectly well for this but decided to mess around with this method because of an article on file i/o I read on this site. It implied that using binary files was a lot easier (ie you don't have to deal with individual data members) and that the only real downside was not being able to real the files easily, lol.

Perhaps I will just go back to using text files.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!