Alright... here it is...
First run the programm and everything is ok. But in second time do comment funcions "save_profile" in main and you get the error...
Even though you after "read_saves" function write that:
temp[0].avatar_spr_ = spr.sprite_;
temp[1].avatar_spr_ = spr2.sprite_;
You still get the error...
#include <iostream>
#include <SFML/Graphics.hpp>
#include <fstream>
#include <io.h>
using namespace std;
using namespace sf;
class SpriteManager{
public:
String file_;
Image image_;
Texture texture_;
Sprite sprite_;
SpriteManager() {}
SpriteManager(String File, int X = 0, int Y = 0){
file_ = File;
image_.loadFromFile("images/" + file_);
texture_.loadFromImage(image_);
sprite_.setTexture(texture_);
sprite_.setPosition(X, Y);
}
};
class profile
{
public:
string name_;
Sprite avatar_spr_;
profile() {};
profile(string name) { name_ = name; }
};
void save_profile(profile & player)
{
ofstream file(player.name_ + ".save", ios_base::out | ios_base::binary);
if (!file.is_open()){
cerr << "Can't open file for output:\n";
exit(EXIT_FAILURE);
}
file.write((char *)&player, sizeof player);
file.close();
}
void read_saves(profile * temp)
{
int hFile, count = 0;
_finddata_t file[2];
if ((hFile = _findfirst("*.save", &file[count])) == -1){
cerr << "Can't open file for output:\n" << endl;
}
else while (_findnext(hFile, &file[++count]) == 0);
ifstream save;
for (int i = 0; i < count; i++){
string temp_str(file[i].name);
save.open(temp_str, ios_base::in | ios_base::binary);
if (!save.is_open()){
cerr << "Can't read a file" << endl;
}
else{
save.read((char *)&temp[i], sizeof temp[i]);
save.close();
}
}
_findclose(hFile);
}
int main()
{
RenderWindow window(VideoMode(900, 450), "Lesson 1. kychka-pc.ru");
View view;
Vector2f pos;
Event event;
SpriteManager spr("image1.png"), spr2("image2.png");
profile player1("First"), player2("Second"), temp[2];
player1.avatar_spr_ = spr.sprite_;
player2.avatar_spr_ = spr2.sprite_;
save_profile(player1);
save_profile(player2);
read_saves(temp);
while (window.isOpen())
{
while (window.pollEvent(event)){
if (event.type == Event::Closed)
window.close();
}
window.clear();
temp[0].avatar_spr_.setPosition(385, 90);
window.draw(temp[0].avatar_spr_);
temp[1].avatar_spr_.setPosition(185, 120);
window.draw(temp[1].avatar_spr_);
window.display();
}
return 0;
}
the natural way to serialize class objects in C++, classes only a type like any other fundamental types int, float. only different with classes is that its size which is varying depends on its member data. for example:
class Foo{};
Foo has size of 1 byte only. this size can grow if you added member data. you don't know what stores in those size of space in memory, it could be address to somewhere in memory locations if it has pointers. so what you are trying to do is not only invalid but dangerous if it runs. please avoid it. don't run this code.
however, if you cared to store raw data for any image format like .png in file. you have to decode it first. most known decoder is base 64. and use it thing like this:
// data is encoded for raw data in .png
std::string decoded = base64_decode(data);
sf::Texture texture;
if (!texture.loadFromMemory(decoded.c_str(), decoded.length()))
{
// handle error
}
i have post thread in here, when i was working in encode/decode for base 64. here link to it (http://en.sfml-dev.org/forums/index.php?topic=19623.msg141279#msg141279)