Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by jaypal (Beadle)
on Jun 17, 2014 at 02:06 UTC ( #1090096=note: print w/ replies, xml ) Need Help??


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

Thank you so much, your improvements to my existing code were great and I am currently trying to modify the code as per your suggestions.

One thing I have to ensure is while I am reading the record and if it has a bad length, it will read in to the next possibly good record and send that to parsing subroutine and will also prevent me from processing the good record as the eye catcher might have probably been read by previous read command.

Will explore more ways and get back to you. Thanks again for great comments.


Comment on Re^3: Reading binary file in perl having records of different length
Re^4: Reading binary file in perl having records of different length
by Anonymous Monk on Jun 17, 2014 at 13:48 UTC

    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".)

      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://1090096]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-08-30 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls