Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^4: Reading binary file in perl having records of different length

by Anonymous Monk
on Jun 17, 2014 at 13:48 UTC ( #1090144=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Reading binary file in perl having records of different length
in thread Reading binary file in perl having records of different length

The algorithm can be modified to handle bad "length" values by adding some logic in the place where it currently dies. If you find a bad record, you could rewind to the last known "good" position via tell and seek and look for the next "==" (an alternative might be to implement your own buffer to look around in, but read should already be buffered). Similar logic would allow you to find "==" that are not aligned properly - the example above doesn't handle the case of the file starting with "x==". (Yet another approach is to read the file byte-by-byte instead of every two bytes - then you would extend your states and have "expect_first_equals", "expect_second_equals", "expect_first_length_byte", and "expect_second_length_byte".)


Comment on Re^4: Reading binary file in perl having records of different length
Download Code
Re^5: Reading binary file in perl having records of different length
by jaypal (Beadle) on Jun 17, 2014 at 15:43 UTC

    Great points!! I think reading one byte at a time is probably the safest. I am a quality assurance engineer and this code won't be ran on production (so I am not reading through a socket where high performance is needed). I will be testing binary files (one at a time) created by my application and have around 60-70 test scenarios that can be automated with this parser that ensures the binary file is constructed correctly.

    tell and seek looks interesting. I will go through the docs and some examples. Thanks again for all your help. You've been great!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1090144]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-08-21 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (134 votes), past polls