From: perl-win32-users-boun...@listserv.activestate.com
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of
Daniel Burgaud
Sent: 05 December 2009 11:50
To: Perl-Win32-Users
Subject: Help: Random Read/Write a binary file

> Hi All
> 
> I have files previously created in binmode.
> 
> ie,
> 
> open SFILE, "filename.dat";
> binmode SFILE;
> print SFILE $buffer;
> close SFILE;
> 
> 
> Now, I want to Append this file somewhere in the middle.
> my $i = (-s "filename.dat") / 2;
> open SFILE, "filename.dat";
> binmode SFILE;
> seek SFILE $i, 0;
> print SFILE $data;
> close SFILE;
> 
> this does not seem to work.
> 
> How do I properly open a file for random read/write?
> 
> 
> if I open it with 
> open SFILE, "filename.dat";
> seek SFILE $i, 0;
> It does not append.
> 
> If I open it with
> open SFILE, ">>filename.dat";
> seek SFILE $i, 0;
> It keeps appending at the end
> 
> if I open it with
> open SFILE, ">>filename.dat";
> seek SFILE $i, 0;
> it writes at that location alright, but first half of data are blank.
> 
> HELP! :(

I hope that is not real code. If it is, you are missing a lot of error
checking.

The answer depends on what you mean by "append in the middle". If you
mean to just overwrite part of your file, the the answer is essentially
a FAQ (see 'perldoc -q update'), but given the apparent age of that
answer I would add advice to localise file handles and use the 3 arg
form of open.

If you mean to insert some data into the file then the general answer is
to simply read from your original file and create a new output file.

HTH

-- 
Brian Raven 
This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to