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;
}