Oct 10, 2001, 1:27 AM
Post #6 of 17
He're my quess.
Re: Ok, I'm stumped again. This time deals withDelete
[In reply to]
You properly need to check my code for some errors, since I typed it in the webbrowser directly. Can can combine my code with other samples above (in threaded view), where new files are created, to store the new data, and then overwrite the original file.
As you can see, the file is never closed, until we're done with it. Meanwhile it's locked, so other programs running at the same moment can't edit the same file at the same time, which results in corrupted data/files.
use Fcntl qw(:DEFAULT :flock);
sysopen(FILE, $Yourfile, O_RDWR) or die "Can't open: $!";
# Get the original data, and change it.
my @FileContents = <FILE>;
my $DeletedItem = splice(@FileContents, $ItemIndex, 1);
# write the new data into the file
seek(FILE, 0, 0) or die "Can't return to beginning: $!";
trucate(FILE, 0) or die "Can't truncate file to zero length: $!";
print FILE @FileContents; # Write the new data into the file.
# unlock so other instances can use the file after out edits.
Hope it helps.