Yes, typical binary stream reading behavior is that the "cursor" or "read head" 
or "offset" or "pointer" moves with reads. You can also usually "seek" to an 
offset various ways to skip over/to different parts of the file rather than 
read through. On semi-sequential media like a platter hard-drive there are 
usually performance gains when data is read sequentially without intermediate 
seeks.

Binary file layout and calculation of offsets for a given file, to best suit 
the purpose of the format, can be a bit of a dark art at times. 


> On Mar 1, 2015, at 10:58 AM, Ben Hearn <[email protected]> wrote:
> 
> Hello all,
> 
> I am currently in the process of writing a tool for work that requires the 
> need to read our in house.model format which is a binary file.
> 
> Here's the thing, the guy who wrote the tools that I inherited never finished 
> them and never commented them so I have been hacking at the old code for a 
> few months now which has been great fun and a brilliant learning process. He 
> left an old file that seems like it reads a binary file which is exactly what 
> I need.
> 
> I am however confused over something involved reading the data in a binary 
> file, so here goes.
> 
> To get the information from the binary file it would appear that certain 
> functions need to be called in a certain order. The only thing is all they 
> essentially do is read the file in byte chunks. If I comment any of the 
> functions out and run the code the data is all wrong.
> 
> So my question is, when you are reading binary files (or any files) every 
> time you perform a read function or a struct.unpack does the position in the 
> file that you read from change? Or is it something simpler like if you have 
> written your binary file in a certain way, you HAVE to read it in exactly the 
> same way?
> 
> I might be missing some crucial theory so any help would be much appreciated 
> :)
> 
> Here are the function calls and the loop that contains the reading of info:
> 
> def _read_uint32(file):
>       data = file.read(4)
>       data = struct.unpack('I', data)[0]
>       return data
>  
> def _read_string(file):
>       size = _read_uint32(file)
>       data = file.read(size)
>       return data
>  
> def _read_int32(file):
>       data = file.read(4)
>       data = struct.unpack('i', data)[0]
>       return data
>  
> def _read_matrix4(file):
>       data = file.read(8 * 16)
>       return data
> 
> And here is the function that I am using to loop through the file and gather 
> the data:
> 
> def unserialize(file):
>  
>       materials = []
>  
>       print "OPENING THIS FILE PATH: ", file
>  
>       file = QtCore.QFile(file)
>  
>       file.open(QtCore.QIODevice.ReadOnly)
>  
>       buf = file.read(4)
>  
>       total_size = _read_uint32(file)
>  
>       start_offset = file.pos()
>  
>       passes = 1
>  
>       for i in range(passes):
>               file.seek(start_offset)
>               
>               while( (file.pos() - start_offset) < total_size):
>                       type_id    = _read_uint32(file)
>                       chunk_size = _read_uint32(file)
>                       
>                       if type_id == 0 and i == 0:
>                               print "ID: ",                           
> _read_int32(file)
>                               print "NAME: ",                         
> _read_string(file)
>                               print "TRANSFORM ",                     
> _read_matrix4(file)
>                               print "PIVOT TRANSFORM ",       
> _read_matrix4(file)
>                               print "PARENT ID ",                     
> _read_int32(file)
>                               print
>                       elif type_id == MATERIAL and i == 0:
>                               print _read_int32(file)
>                               print _read_string(file)
>                               print _read_int32(file)
>                       else:
>                               file.seek(file.pos() + chunk_size)
> 
> Cheers!
> 
> Ben
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/python_inside_maya/45b8fdf5-c7e4-4f86-8dbc-f575659cf585%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/14226A1B-8136-487C-AC51-B75C55D6403B%40fie.us.
For more options, visit https://groups.google.com/d/optout.

Reply via email to