i have a function saving items to a file and reading items from a file. the program is incorectly reading the statbuffs in the vectors of the items. i've been looking for my coding error for a while and can't find it, so i'm probably doing something wrong and don't even know it. if anyone can help i would really appreciate it. and i thank everyone for all the help i've received here so far.
Code:
void load_items(ifstream *file)
{
char test = file->get();
while (!file->eof())
{
file->putback(test);
item *load = new item;
cout << endl << endl << "+++ NEW ITEM +++" << endl << endl;
load->set_slot(file->get()); cout << static_cast<int>(load->get_slot()) << endl;
load->set_wsprite(bin_read_2bytes(file)); cout << load->get_wsprite() << endl;
load->set_asprite(bin_read_2bytes(file)); cout << load->get_asprite() << endl;
load->set_value(bin_read_4bytes(file)); cout << load->get_value() << endl;
load->set_inventoryFrame(file->get()); cout << static_cast<int>(load->get_inventoryFrame()) << endl;
load->set_prof(file->get()); cout << static_cast<int>(load->get_prof()) << endl;
load->set_sizeStatBuff(file->get()); cout << static_cast<int>(load->get_sizeStatBuff()) << endl;
load->set_name(bin_read_string(file)); cout << load->get_name() << endl;
load->set_desc(bin_read_string(file)); cout << load->get_desc() << endl;
for (test = 0; test < load->get_sizeStatBuff(); test++)
{
cout << " +++ " << load->get_size() << " +++ " << endl;
statBuff *temp = new statBuff;
temp->set_stat(file->get()); cout << static_cast<int>(temp->get_stat()) << endl;
temp->set_amount(file->get()); cout << static_cast<int>(temp->get_amount()) << endl;
load->add_statBuff(temp);
}
test = file->get();
}
}
int main()
{
ofstream fout;
ifstream fin;
item jake(SLOT_NOONE, 0, 0, 0, 0, PROF_NOONE, 0, "", "");
item_save(&jake);
item billy(SLOT_RHAND, 0, 0, 52, 0, PROF_SWORD, 3, "sword", "a simple sword");
statBuff *temp = new statBuff;
temp->set_stat(WEAP_RANGE);
temp->set_amount(1);
billy.add_statBuff(temp);
statBuff *temp1 = new statBuff;
temp->set_stat(WEAP_SPEED);
temp->set_amount(1);
billy.add_statBuff(temp1);
statBuff *temp2 = new statBuff;
temp->set_stat(WEAP_POWER);
temp->set_amount(3);
billy.add_statBuff(temp2);
item_save(&billy);
delete temp, temp1, temp2;
fin.open("idb.dat", ios::in | ios::binary);
load_items(&fin);
fin.close();
char i;
cin >> i;
return (0);
}
Code:
// items.h
enum SLOTS{SLOT_NOONE, SLOT_HEAD, SLOT_NECK, SLOT_SHOULDERS, SLOT_CHEST, SLOT_BELT, SLOT_LEGS, SLOT_FEET, SLOT_WRISTS, SLOT_HANDS, SLOT_BACK, SLOT_RING, SLOT_RHAND, SLOT_LHAND, SLOT_RANGED, SLOT_AMMO};
class statBuff
{
public:
statBuff(){_stat = 0; _amount = 0;}
char get_stat() const {return (_stat);}
void set_stat(char stat){_stat = stat;}
char get_amount() const {return (_amount);}
void set_amount(char amount){_amount = amount;}
private:
unsigned char _stat;
unsigned char _amount;
};
class item
{
public:
item(){}
item(char slot, unsigned int wsprite, unsigned int asprite, unsigned long int value, unsigned char frame, unsigned char prof, unsigned char size, string name, string desc):_name(name), _desc(desc){_slot = slot; _wsprite = wsprite; _asprite = asprite; _value = value; _inventoryFrame = frame; _prof = prof; _sizeStatBuff = size;}
char get_slot() const {return (_slot);}
void set_slot(char slot){_slot = slot;}
unsigned int get_wsprite() const {return (_wsprite);}
void set_wsprite(unsigned int si){_wsprite = si;}
unsigned int get_asprite() const {return (_asprite);}
void set_asprite(unsigned int si){_asprite = si;}
unsigned long int get_value() const {return (_value);}
void set_value(unsigned long int value){_value = value;}
unsigned char get_inventoryFrame() const {return (_inventoryFrame);}
void set_inventoryFrame(unsigned char frame){_inventoryFrame = frame;}
unsigned char get_prof() const {return (_prof);}
void set_prof(unsigned char prof){_prof = prof;}
unsigned char get_sizeStatBuff() const {return (_sizeStatBuff);}
void set_sizeStatBuff(unsigned char size){_sizeStatBuff = size;}
string get_name() const {return (_name);}
void set_name(string name){_name = name;}
string get_desc() const {return (_desc);}
void set_desc(string desc){_desc = desc;}
statBuff* get_statBuffAt(int index) const {return (_statBuffs[index]);}
void set_statBuffAt(int index, statBuff *sb){_statBuffs[index] = sb;}
void add_statBuff(statBuff *sb){_statBuffs.push_back(sb);}
size_t get_size() const {return (_statBuffs.size());}
private:
char _slot;
unsigned int _wsprite;
unsigned int _asprite;
unsigned long int _value;
unsigned char _inventoryFrame;
unsigned char _prof;
unsigned char _sizeStatBuff;
string _name;
string _desc;
vector<statBuff*> _statBuffs;
};
void item_save(item *save)
{
ofstream file;
file.open("idb.dat", ios::out | ios::binary | ios::app);
file << save->get_slot();
bin_write_2bytes(&file, save->get_wsprite());
bin_write_2bytes(&file, save->get_wsprite());
bin_write_4bytes(&file, save->get_value());
file << save->get_inventoryFrame();
file << save->get_prof();
file << save->get_sizeStatBuff();
bin_write_string(&file, save->get_name());
bin_write_string(&file, save->get_desc());
for (char i = 0; i < save->get_sizeStatBuff(); i++)
{
statBuff *sb = save->get_statBuffAt(i);
file << sb->get_stat();
file << sb->get_amount();
}
}
i think the problem could be with the data types i'm storing the varialbes with, but i really don't know. thank you for your time.