Reading char arrays from a binary file

This is a discussion on Reading char arrays from a binary file within the C++ Programming forums, part of the General Programming Boards category; If you still want to use read(), this is what I use and it works just fine: Code: string LoadText(string ...

  1. #16
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    If you still want to use read(), this is what I use and it works just fine:
    Code:
    string LoadText(string fileName)
    {
    	int length;
    	char * out;
    	string output;
    	ifstream is;
    	is.open (fileName.c_str(), ios::binary );
    
    	// get length of file:
    	is.seekg (0, ios::end);
    	length = is.tellg();
    	is.seekg (0, ios::beg);
    
    	// allocate memory:
    	out = new char [length];
    
    	// read data as a block:
    	is.read (out,length);
    	is.close();
    	output.assign(out, length);
    	return output;
    }
    >>I want to avoid using a text file. if possible.

    You read the file the same way no matter what type of file it is.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> If you still want to use read(), this is what I use and it works just fine:
    Unfortunately it has a memory leak, which is why I suggested vector.

  3. #18
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Well here is my finished project...after two day of frustration

    Code:
    void CLoadLevel::LoadLevel(std::string filename)
    {
        /*Temeporay storage vector for reading in character arrays form a binary file*/
        std::vector<char>temp(0);
        /*Local variables for reading in int data from a binary file*/
        char ch;
        int namelength;
        int strength;
        int dexterity;
        int intelligence;
        int endurance;
        int hp;
        int maxhp;
        int mp;
        int maxmp;
        int gold;
        int xp;
        int weaponname;
        int mindamage;
        int maxdamage;
        int accuracy;
        int price;
        int armorname;
        int ap;
    
        std::ifstream in(filename.c_str(), std::ios::binary);
    
        in.read((char*)&enemycount, sizeof(int));
    
        for(int count = 0; count < enemycount; count++)
        {
            in.read((char*)&namelength, sizeof(int));
    
            for(int x = 0; x < namelength; x++)
            {
                in.get(ch);
                temp.push_back(ch);
            }
            std::string name(temp.begin(), temp.end());
            temp.clear();
    
            in.read((char*)&strength, sizeof(int));
            in.read((char*)&dexterity, sizeof(int));
            in.read((char*)&intelligence, sizeof(int));
            in.read((char*)&endurance, sizeof(int));
            in.read((char*)&hp, sizeof(int));
            in.read((char*)&maxhp, sizeof(int));
            in.read((char*)&mp, sizeof(int));
            in.read((char*)&maxmp, sizeof(int));
            in.read((char*)&gold, sizeof(int));
            in.read((char*)&xp, sizeof(int));
    
            in.read((char*)&namelength, sizeof(int));
            for(int x = 0; x < namelength; x++)
            {
                in.get(ch);
                temp.push_back(ch);
            }
            std::string weaponname(temp.begin(), temp.end());
            temp.clear();
    
            in.read((char*)&mindamage, sizeof(int));
            in.read((char*)&maxdamage, sizeof(int));
            in.read((char*)&accuracy, sizeof(int));
    
            /*Read in the number of characters in armorname*/
            in.read((char*)&namelength, sizeof(int));
    
            /*Now read in armorname*/
            for(int x = 0; x < namelength; x++)
            {
                in.get(ch);
                temp.push_back(ch);
            }
            std::string armorname(temp.begin(), temp.end());
            temp.clear();
    
            in.read((char*)&ap, sizeof(int));
    
            CEnemy enemylist;
            enemylist.CreateEnemy(name, strength, endurance, dexterity, intelligence, hp, maxhp, mp,
                                        maxmp, xp, gold, weaponname, mindamage, maxdamage, accuracy, armorname, ap);
    
            enemy.push_back(enemylist);
        }
    
        in.read((char*)&weaponcount, sizeof(int));
    
        for (int count = 0; count < weaponcount; count++)
        {
            in.read((char*)&namelength, sizeof(int));
    
            for(int x = 0; x < namelength; x++)
            {
                in.get(ch);
                temp.push_back(ch);
            }
            std::string newname(temp.begin(), temp.end());
            temp.clear();
    
            in.read((char*)&mindamage, sizeof(int));
            in.read((char*)&maxdamage, sizeof(int));
            in.read((char*)&accuracy, sizeof(int));
            in.read((char*)&price, sizeof(int));
    
            CWeapon weaponlist;
            weaponlist.CreateWeapon(newname, mindamage, maxdamage, accuracy, price);
    
            weapon.push_back(weaponlist);
        }
    
        in.close();
    };
    It now works perfectly...as far as I can tell...unfortunately I will probably be overhauling it agian soon.

    Anyway thank you Daved, Swoopy, mikeman and to the resst of you who gave advice.
    Last edited by eaane74; 11-20-2007 at 04:27 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21