I'm trying to take all my sprites and put them into one file. The problem is when I go to read them it always returns a NULL SDL_Surface. So I must be doing something wrong, but I don't know what it is, so I figured I'd ask here.

I start with writing a sprite from a file with a code like:
PHP Code:
 ofstream fout;
    
fout.open("res.epe"ios::binary);

    
bin_res_start(&fout1);

    
bin_write_sprite(&fout"spr_blank.gif"0IT_GIF11);

    
fout.close(); 
Then I read the sprite like this:
PHP Code:
 ifstream fin;
    
fin.open("res.epe");

    
int s bin_read_sprite(&fin0);

    
fin.close(); 
Here is all the code:
PHP Code:
 // turns a 0-65535 integer into 2 bytes
char *pack(unsigned int valuechar buffer[2])
{
    
buffer[0] = (value >> 8);
    
buffer[1] = (value 255);

    return (
buffer);
}
// turns 2 bytes into a 0-65535 integer
unsigned int unpack(char byteAchar byteB)
{
    return ((
static_cast<unsigned int>(byteA) << 8) | byteB);
}
// heads the file
// rs is the number of sprites that are going to be put into the file
void bin_res_start(ofstream *foutunsigned int rs)
{
    
char p[2];

    *
fout << pack(rsp);

    
unsigned int i 0;

    while (
< (rs*9))
    {
        *
fout << '\0';
        
i++;
    }
}
// write a sprite into the file as well as its type and width and height
// rn is the index of the sprite in the file (if its the 4th sprite to be put in the file its index is 3)
void bin_write_sprite(ofstream *foutstring fnameint rnunsigned int typeunsigned int wunsigned int h)
{
    
char p[2]; // used to pack integers into bytes

    
ifstream fin// open the sprite
    
fin.open(fname.c_str(), ios::in|ios::binary);

    
fout->seekp(0ios::end); // find the file size of the current file
    
unsigned int fsizeFout fout->tellp();

    
fin.seekg(0ios::end); // find the file size of the sprite
    
unsigned int fsizeFin fin.tellg();

    
fin.seekg(0); // seek to the position where the information about the sprite will be written
    
fout->seekp((rn*9) + 2);

    *
fout << static_cast<unsigned char>(type); // write the type of sprite (GIF, PNG, etc..)
    
*fout << pack(wp); // write the width of the sprite
    
*fout << pack(hp); // write the height of the sprite
    
*fout << pack(fsizeFoutp); // write the current file size (thats where the actual sprite will be written at, this way I can seek there to read it)
    
*fout << pack(fsizeFinp); // write the file size of the sprite (so that I can loop through to read it into memory)

    
string buffer// to store the contents of the sprite

    
unsigned int i 0;

    while (
fsizeFin// loop through the sprite and add it to buffer
    
{
        
buffer += fin.get();
        
i++;
    }

    
fout->seekp(0ios::end); // seek to the end of the file to append the sprite

    
*fout << buffer// write the sprite

    
fin.close();
}
// reads a sprite from a res file
// rn is the index of the file to be read
int bin_read_sprite(ifstream *finint rn)
{
    
fin->seekg((rn*9) + 2); // seek to the position where all the information about the sprite is stored

    
unsigned char type fin->get(); // get the type
    
unsigned int w unpack(fin->get(), fin->get()); // get the width
    
unsigned int h unpack(fin->get(), fin->get()); // get the height
    
unsigned int b unpack(fin->get(), fin->get()); // get the starting location of the sprite
    
unsigned int e unpack(fin->get(), fin->get()); // get the file size of the sprite

    
SDL_RWops *rw;

    
string buffer;

    
fin->seekg(b); // seek to the position of the sprite

    
int i 0;

    while (
e// read the sprite into a string
    
{
        
buffer += fin->get();
        
i++;
    }

    
rw SDL_RWFromConstMem(buffer.c_str(), e); // set the RWops to the buffer

    
return load_sprite(rwtypewh); // load the sprite
}
// load the sprite from a RWops
// type is the type of image (GIF, PNG, etc..)
// w is the width of the sprite
// h is the height of the sprite
int load_sprite(SDL_RWops *rwint typeint widthint height)
{
    
sprite *= new sprite;

    
s->set_sprite(rwtypewidthheight);
    
engine::sprVector.push_back(s);

    return 
engine::sprVector.size();
}
class 
sprite
{
  public:
    
sprite()
    {
        
si++; // index
        
_si si;
    }
    ~
sprite()
    {
        
int
        cap 
_frameVector.size(),
        
i   0;

        while (
cap// delete allocated memory
        
{
            
delete _frameVector[i];
            
i++;
        }
    }

    
void set_sprite_from_sprite(SDL_Surface *_spriteint widthint height// create new SDL_rects for the frames of the sprite
    
{
        
int i 11;

        while (
width*<= _sprite->w)
        {
            while (
height*<= _sprite->h)
            {
                
SDL_Rect *= new SDL_Rect;
                
r->width*(1);
                
r->height*(1);
                
r->width;
                
r->height;

                
_frameVector.push_back(r);

                
j++;
            }
            
1;
            
i++;
        }
    }

    
void set_sprite(SDL_RWops *rwint typeint width 0int height 0// set the _sprite to an image
    
{
        
_sprite load_image(rwtype);

        
set_sprite_from_sprite(_spritewidthheight);
    }

    
void set_sprite(string filenameint width 0int height 0)
    {
        
_sprite load_image(filename.c_str());

        
set_sprite_from_sprite(_spritewidthheight);
    }

    
SDL_Surface get_sprite() const
    {
        return 
_sprite;
    }

    
SDL_Rect get_frame(int index) const
    {
        if (
index _frameVector.size())
        {
            return 
_frameVector[index];
        }
        else
        {
            return 
NULL;
        }
    }

  private:
    
SDL_Surface *_sprite;
    
int         _width;
    
int         _height;
    
int         _si;

    
vector<SDL_Rect*>_frameVector;

    static 
int si;
};
SDL_Surface *load_image(SDL_RWops *rwint type)
{
    
SDL_Surface *loadedImage    NULL// temp storage for the image being loaded
    
SDL_Surface *optimizedImage NULL// the optimized image; image that will be used

    
switch (type)
    {
        case 
IT_PNG:
            
loadedImage IMG_LoadPNG_RW(rw);
            break;
        case 
IT_GIF:
            
loadedImage IMG_LoadGIF_RW(rw);
            break;
    }

    if (
loadedImage != NULL)
    {
        
optimizedImage SDL_DisplayFormat(loadedImage); // optimize the image; change its bpp to the screen's

        
SDL_FreeSurface(loadedImage); // free the non-optimized image

        
if (optimizedImage != NULL)
        {
            
Uint32 colorkey =  SDL_MapRGB(optimizedImage->format00xFF0xFF);

            
SDL_SetColorKey(optimizedImageSDL_RLEACCEL SDL_SRCCOLORKEYcolorkey);
        }
    }

    return 
optimizedImage;

I know this is a lot to look through, but I've been trying to find the bug for a few days and I'm getting no where so I would really appreciate it if someone who knows what they are doing could lend a hand. Thank you for your time.