PDA

View Full Version : Open file problems


Richard
06-26-2004, 11:56 PM
I want to make a script that will read a file* which only contains a number. I then want to increment the number by 1, ie the number plus 1. Then I want it to delete the existing number and write the new number to the file.

* It must check if the file exists first, and if not, create one and write the number '1' to it.

Can anyone help with this, please? My knowledge of perl is extremely basic.

YUPAPA
06-30-2004, 03:20 PM
use sysopen to open a file...


my $num = 0;

sysopen(FILE, 'myfile', O_RDONLY|O_CREAT);
$num = <FILE>;
close(FILE);

sysopen(FILE, 'myfile', O_RDWR|O_CREAT);
print FILE ++$num;
close(FILE);


Haven't tested the code, but it should work.