Thread: problem with reading and writing

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    problem with reading and writing

    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.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    This is a good case study in why human readable file formats are nice. My guess would be that bin_read_string goes too far and consumes some of your stat data.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    no, the strings are read fine, as is everything before the strings. the problem is either how the data for the statbuffs are being added, stored, saved, or loaded. i've tried changing a few things, but nothing fixes the problem. bin read string just reads until it finds the \0 character.

    Code:
    string bin_read_string(ifstream *file)
    {
        char chr, i;
        file->get(chr);
        string str = "";
        
        while (chr != '\0')
        {
            str += chr;
    	file->get(chr);
        }
    
        return (str);
    }
    
    void bin_write_string(ofstream *file, string str)
    {
        *file << str;
        *file << '\0';
    }

    i found the reason for the bug. thank you for all your help.
    Last edited by yahn; 01-03-2006 at 09:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. reading file problem
    By samsam1 in forum Windows Programming
    Replies: 4
    Last Post: 01-15-2003, 06:03 PM