Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read bytes from a memory mapped file

Read bytes from a memory mapped file

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neliocc
    wrote on last edited by
    #1

    Hi everybody. I have a problem. I have a file mapped in memory, and a pointer to section of that file, and i need to read n bytes from the file in memory begining in the pointer. If anybody can helpme i will be gratefull.

    M A 2 Replies Last reply
    0
    • N neliocc

      Hi everybody. I have a problem. I have a file mapped in memory, and a pointer to section of that file, and i need to read n bytes from the file in memory begining in the pointer. If anybody can helpme i will be gratefull.

      M Offline
      M Offline
      Matt Godbolt
      wrote on last edited by
      #2

      If the file's memory mapped, then you treat it exactly like memory - so 'reading n bytes' from the file is simply a memcpy(). If you want to emulate file-like behaviour, try something like (untested code!):

      // Constants, you'll need to set these up. Ideally you'd wrap these in a class somewhere
      // and treat them like a FILE* object; this example only supports one 'open' file
      const char* gFileBase = address at the beginning of your memory mapped file;
      size_t gFileSize = size of the file that was memory mapped;

      size_t filePtr = 0;
      int memfread(void* destination, size_t numBytes)
      {
      // Clamp to the end of the 'file'.
      size_t bytesRemaining = gFileSize - filePtr;
      if (numBytes > bytesRemaining)
      numBytes = bytesRemaining;

      // Copy the number of bytes requested out.
      memcpy(destination, gFileBase + filePtr, numBytes);
      
      // Move the virtual file pointer on.
      filePtr += numBytes;
      
      return numBytes;
      

      }

      Something like that anyway - you can hopefully see how you might implement memfseek() and so forth. Of course, ideally you'd reap the most benefits from a memory mapped file simpply by treating it pure memory. For example, if your file was an array of Foo structures:

      // Get the base of the foo array.
      Foo* fooArray = reinterpret_cast<Foo*>(gFileBase);
      // use fooArray as if you'd loaded them normally
      // any changes you make are reflected on disk automatically.
      // e.g. things like "foo[12].bar = 12;" and so forth

      Matt Godbolt Engineer, ProFactor Software StyleManager project -- modified at 7:06 Saturday 17th September, 2005

      1 Reply Last reply
      0
      • N neliocc

        Hi everybody. I have a problem. I have a file mapped in memory, and a pointer to section of that file, and i need to read n bytes from the file in memory begining in the pointer. If anybody can helpme i will be gratefull.

        A Offline
        A Offline
        Alexander M
        wrote on last edited by
        #3

        Create a class that has is initialized with a pointer to that section and it's size. Every read request will increase the pointer by the amount of bytes read. If that amount gets bigger than the section's size, the EOF is reached. Don't try it, just do it! ;-)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups