Issue with opening file

Posted by aggressive_fish on 2010-08-25 06:59

Hi All,
I'm "trying" to read/write a file using the open function and +> without success. When I try to open the file this way and read the contents, it doesn't appear the read is working. I try to assign the contents of the read to an array such as :

open (FILEHANDLE, "+>$variablePath");
my @ContentArray = ;
print join("", @ContentArray);
...
...

Nothing prints to the screen. Any ideas on what is wrong? Alternatives to this approach (i.e. better way to do it)?

Once I read the contents I need to make adjustments to the contents of the data retrieved and write it back to the same file. Also I need to lock the file when I read and then release when I write. Any additional help/suggestions on that would be much appreciated as well.

AF

inmalathi | Fri, 2010-09-03 09:09

Hi,

The following code would work as you expected:

#!/usr/bin/perl
$variablePath = 'audit.tmp';
open(FILEHANDLE, "+<$variablePath");
my @ContentArray = ;
print join("", @ContentArray);
# The cursor would be at the end of the file after reading all the lines.

print FILEHANDLE "This is the final line in this document\n";
#the above line would be appended at the end of the 'audit.tmp' file.