Hi, I recently started learning C++ because i wan 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

Heres 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 ka1("Name2",18,17,18);
	Kaizoku ka00;
	Kaizoku ka11;
	
	ka0.print();
	ka00.print();
	
	ka0.save("kaizoku.txt");
	
	cout << "AFTER SAVE" << endl;
	ka00.load("kaizoku.txt");
	
	ka00.print();
	
}

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

What does it tell you when it crashes? I don't see a return statement in your main(), but I wouldn't expect a crash because of it...

I get the window that says program.exe has stopped working, here are the details:

Problem signature:
Problem Event Name: APPCRASH
Application Name: filesIO.exe
Application Version: 0.0.0.0
Application Timestamp: 4c76b4a8
Fault Module Name: MSVCP100D.dll
Fault Module Version: 10.0.30319.1
Fault Module Timestamp: 4ba1dbdf
Exception Code: c0000005
Exception Offset: 0000ad54
OS Version: 6.1.7600.2.0.0.256.1
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

The error is in the MS Visual C++ runtime.

Add

return 0;

@ Line 25. See if it makes a difference.

Just added it, still crashes =(.
Also it only crashes if i use the load funtion, but i don't get any errors or warnings after compiling.

I don't think you are reading and writing your data correctly causing your read method to corrupt your this pointer. Does the file "kaizoku.txt" exist after you attempt to run the program? If so, please post the contents of it... I suspect the contents are not what you think.

Here is the content of the file, its in binary though:

1 Name1 ÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌ

Member Avatar for embooglement

Why not read and write each member individually? Something like this:

void Kaizoku::save(string fileName)
{
	ofstream oFile(fileName,ios_base::binary);
	
	oFile.clear();
	if(oFile)
	{
	        oFile << this->dName;
	        oFile << this->dHitPoints;
	        oFile << this->dStamina;
	        oFile << this->dSpeed;

		oFile.close();
	}
}

Not 100% sure if that's right, I don't generally do binary i/o, but the idea is to avoid trying to interpret the this pointer as a character array.

...its in binary though:
...

Oops, I forgot about that....

Why not read and write each member individually? Something like this:

void Kaizoku::save(string fileName)
{
	ofstream oFile(fileName,ios_base::binary);
	
	oFile.clear();
	if(oFile)
	{
	        oFile << this->dName;
	        oFile << this->dHitPoints;
	        oFile << this->dStamina;
	        oFile << this->dSpeed;

		oFile.close();
	}
}

Not 100% sure if that's right, I don't generally do binary i/o, but the idea is to avoid trying to interpret the this pointer as a character array.

this is exactly what I was getting at...

Yeah it seems i can't save std::string like that since it contains a pointer to the underlying string data. So i saved them individually like embooglement suggested, and i saved the string as a c-string and i had no problems loading the data from the file, all i had to do was convert it back to string. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.