Thread: Please Help Me With File Into Memory

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Unhappy Please Help Me With File Into Memory

    Hey,

    I really need to work out how to do this, i need to basically load a .rar file into the memory so i can decompress it, currently i am loading the rar file from a resource into the memory then decompressing it, but i need to load it from a file rather than resource.

    Heres how im loading it from resource:

    Code:
    void openRARResource(MemoryFile *memfile, int resource_id)
    {
      HRSRC hsResourceFH;            
      HGLOBAL hgResourceLH;
      HINSTANCE hInstance  = GetModuleHandleA(NULL); 
    
      memfile->data   = 0;                     
      memfile->size   = 0;
      memfile->offset = 0;
    
      hsResourceFH = FindResource(hInstance, MAKEINTRESOURCE(resource_id), "RARFILE");
    
      if(hsResourceFH)
      {
        hgResourceLH = LoadResource(hInstance, hsResourceFH);
        if(hgResourceLH)
        {
           memfile->size = SizeofResource(hInstance, hsResourceFH);
           memfile->data = (void*)LockResource(hgResourceLH);
        }
      } 
    
    }

    This is how its called:

    Code:
    MemoryFile rarfile;
    openRARResource(&rarfile, IDR_RAR);

    This is how MemoryFile Is defined:

    Code:
    typedef struct  memory_file               
    {                                          
      void *data;         /* pointer to the file data     */
      unsigned long size;          /* total size of the file data  */
      unsigned long offset;        /* offset within "memory-file"  */  
    } MemoryFile;
    You can see there that i need the file data, file size, and memory offset...


    I would like if possible to load the file into the MemoryFile varible, but as i said from a .rar on the hdd rather than from the programs resources.

    I would majorly appreciate any help,

    Thanks guys
    TNT
    Last edited by (TNT); 06-21-2002 at 11:33 AM.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    since i don't know an enormous amount about the rar format, try this.

    NOTE: if the file is large, you might want to manage it in small chunks if thats possible, this is for one large chunk.

    Code:
    int LoadRar(char* filename, MemoryFile* rar)
    {
        // open file
        FILE* fp = fopen(filename,"rb");
        // check to see if it opened
        if(!fp)
            return -1;
    
        memset(rar,0,sizeof(MemoryFile));
    
        // find the end of the file
        fseek(fp,0,SEEK_END);
    
        // get the length in bytes by using the file position.
        rar.size = ftell(fp);
    
        // allocate memory
        rar.data = (void*) malloc(rar.size);
    
        // go back to the beginning of the file
        fseek(fp,0,SEEK_SET);
    
        // read the data an check for errors
        if(fread(rar.data,1,rar.size,fp) < rar.size)
        {
            free(rar.data);
            rar.size = 0;
            return -1;
        }
    
        return rar.size;
    }
    you might wanna add some more error checking maybe for the memory allocation, and like i said you might manage the file in chunks if its to big.
    Last edited by no-one; 06-21-2002 at 12:23 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Writing input from a file into shared memory
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 12:34 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM