Jump to content
  • Advertisement
Sign in to follow this  
mdl731

fstream problem

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

Hi, I recently started learning C++ because i want to develop games, anyway i just got to file i/o part and i got a problem with the binary file read.

I created a class to test the binary file i/o:

#ifndef KAIZOKU_H
#define KAIZOKU_H

#include <string>
#include <fstream>

class Kaizoku
{
public:
Kaizoku();
Kaizoku(std::string name, int hitPoints, int stamina, int speed);

void print();

void save(std::string fileName);
void load(std::string fileName);

private:
std::string dName;
int dHitPoints;
int dStamina;
int dSpeed;
};

#endif


Here is the class implementation:

 
#include "Kaizoku.h"
#include <iostream>

using namespace std;

Kaizoku::Kaizoku()
{
this->dName = "Default";
this->dHitPoints = 0;
this->dStamina = 0;
this->dSpeed = 0;
}

Kaizoku::Kaizoku(string name, int hp, int stamina, int speed)
{
this->dName = name;
this->dHitPoints = hp;
this->dStamina = stamina;
this->dSpeed = speed;
}

void Kaizoku::print()
{
cout << "Kaizoku Name: " << this->dName << endl;
cout << "HP: " << this->dHitPoints << endl;
cout << "Stamina: " << this->dStamina << endl;
cout << "Speed: " << this->dSpeed << endl <<endl;
}

void Kaizoku::save(string fileName)
{
ofstream oFile(fileName,ios_base::binary);

oFile.clear();
if(oFile)
{
oFile.write((char*)this,sizeof(Kaizoku));

oFile.close();
}
}

void Kaizoku::load(string fileName)
{
ifstream iFile(fileName,ios_base::binary);

if(iFile)
{
iFile.read((char*)this, sizeof(Kaizoku));

iFile.close();
}

}


And heres my main:

 
#include <iostream>
#include <string>
#include "Kaizoku.h"

using namespace std;

int main()
{
Kaizoku ka0("Name1",20,15,20);
Kaizoku ka00;

ka0.print();
ka00.print();

ka0.save("kaizoku.txt");

cout << "AFTER SAVE" << endl;
ka00.load("kaizoku.txt");


ka00.print();

return 0;
}


The program saves fine, the problem i got is that it crashes at the end of the application when i load the file and i have no idea why?

Share this post


Link to post
Share on other sites
Advertisement
You cannot save most structures like that, and these days, even when it would work, you generally should not. You especially cannot save one that contains a std::string object that way, because the string object contains a pointer (to the underlying string data), and possibly other pointers (for other reasons), and writing pointers to a file doesn't work because you have no record of the actual pointed-to data (and no guarantee that it will be there, valid, and in the same place when you load the pointers).

You must write each data member of the file separately, and you must use some kind of encoding for the string (since you cannot access its data members in order to write them separately, and since that is still no use as some of them are pointers).

Read this for an explanation of the techniques involved here.

By the way, what does 'd' stand for on your member names?

Share this post


Link to post
Share on other sites
Thank you :D, it is a relief to know what the problem was, i'll work on it now. About the d's, i usually name my class members with a d at the start, for easy access i suppose.

By the way can you please recomend me some good C++ and game programming books.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!