Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^14: Question about binary file I/O

by BrowserUk (Patriarch)
on Mar 05, 2011 at 15:33 UTC ( [id://891573]=note: print w/replies, xml ) Need Help??


in reply to Re^13: Question about binary file I/O
in thread Question about binary file I/O

I was hoping I could do it without needing seek(),

Why not just hide the seek inside a function of your own? Something like:

sub printAt { my( $fh, $src, $nChars, $offset ) = @_; seek $fh, $offset, 0; return print $fh substr $src, 0, $nChars; } printAt( $fh, $s, 4, 1234 );

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^15: Question about binary file I/O
by TheMartianGeek (Novice) on Mar 05, 2011 at 22:11 UTC
    Actually, that's a good idea. The problem with that, though, is that the print function completely erases the rest of the file. Now that I think about it, I don't know if I ever found a good way to overwrite characters in a file without erasing the rest of the file...
      The problem with that, though, is that the print function completely erases the rest of the file.

      It won't if you use the right open mode; ie. '+<':

      #! perl -sw use strict; ## Open for reading and writing open IO, '+<', 'junk.dat' or die $!; seek IO, 11, 0; print IO 'this overwrites'; close IO; __END__ C:\test>type junk.dat xxxxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxx C:\test>junk59 C:\test>type junk.dat xxxxxxxxx this overwritesxxxxx xxxxxxxxx

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Oh, I see. That's useful.

        Okay, my next question is kind of hard to describe...basically, I want to find all blocks of data in a file that are composed of a single specified character and have a specified minimum length. (Say, all blocks of character 0x20 that are at least 12 bytes long.) BUT, said blocks a) shouldn't cross certain boundaries (say, they shouldn't overlap between groups of 1000 bytes) and b) shouldn't be taken into account if they are part of a reserved block marked with a certain keyword (say, the ASCII characters "U S E D" followed by the block length). I don't really even know where to start on this...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found