Thread: Please Help Me With File Into Memory

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001

    Unhappy Please Help Me With File Into Memory


    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:

    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");
        hgResourceLH = LoadResource(hInstance, hsResourceFH);
           memfile->size = SizeofResource(hInstance, hsResourceFH);
           memfile->data = (void*)LockResource(hgResourceLH);

    This is how its called:

    MemoryFile rarfile;
    openRARResource(&rarfile, IDR_RAR);

    This is how MemoryFile Is defined:

    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
    Last edited by (TNT); 06-21-2002 at 11:33 AM.
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    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.

    int LoadRar(char* filename, MemoryFile* rar)
        // open file
        FILE* fp = fopen(filename,"rb");
        // check to see if it opened
            return -1;
        // find the end of the file
        // get the length in bytes by using the file position.
        rar.size = ftell(fp);
        // allocate memory = (void*) malloc(rar.size);
        // go back to the beginning of the file
        // read the data an check for errors
        if(fread(,1,rar.size,fp) < rar.size)
            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