2

My target is to read a binary file using mmap() and a class. The issue is that the data I want to get from the file is not in byte position 0, and the offset is 24. If I set this offset to the mmap function mmap.mmap(fd, length, access, offset = 24) an error is raised, since offset has to be a multiple of granularity. My code is:

class StructHeader(Structure):
    _pack_ = 1
    _fields_ = [('nothing', c_char*24),('v1', c_ubyte),('v2', c_ubyte)]

d_arrayHeader = StructHeader*1

if __name__ == '__main__':
    fd = os.open(filePath, os.O_RDWR)
    granularity = mmap.ALLOCATIONGRANULARITY

    mmap_file = mmap.mmap(fd, length=187, access=mmap.ACCESS_WRITE, offset=0)

    data = d_arrayHeader.from_buffer(mmap_file)

    i = data[0]

    print i.v1, i.v2

I thought three solutions:

  • Getting the whole mmap and later data = d_arrayHeaderLAS.from_buffer(mmap_file[24:]) where the argument of from_buffer() is a subarray of mmap. The issue is that this subarray is transformed from a mmap object to str, and it doesn't work.
  • The second solution was to add a new field with a length of 24 bytes (as showed in line 3 of code).
  • I know how to do it with struct.unpack(), but I wouldn't like to use it, because it is slower.

I'd like to know if there is an easier way to get a mmap with an offset. This case I showed is easy, but I'd like to use it in more complicated cases, such as getting data from binary file where the offset is equal to the header. In these cases, header and data structures are differents, and I should use different classes. And I need it is fast, since files are too big.

Thank you.

2
  • 1
    from_buffer() accepts offset: from_buffer(mmap_file, 24). Commented Nov 30, 2016 at 10:00
  • Thank you! I didn't know it was so easy =) Commented Nov 30, 2016 at 10:15

1 Answer 1

1

You can use "mmap_file.seek(0)" to make file pointer at offset zero ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it works too, but @acw1668 replied that I can use from_buffer(mmap_file, offset)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.