shahrahulb
asked on
file
i have a filr test.txt
cat test.txt
3
it only contains a integer value....i want to increment the value
is it possible to do it whithout opening the file, then reading the current value, then open in write mode.
is it possible to do in just 1 step
Rahul
cat test.txt
3
it only contains a integer value....i want to increment the value
is it possible to do it whithout opening the file, then reading the current value, then open in write mode.
is it possible to do in just 1 step
Rahul
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Maybe you can try appending a line to the file to increment, and counting the number of lines to read the counter.
You could use the filename itself as the integer value and increment that. However if this were the case the filename would change everytime and therefore you would need to perform a readdir every time and look for your text file and then increment and recreate the new file by opeing it anyway and then closing it and then remove the old file.
__________________________ ________
#!/usr/bin/perl -w
use strict;
opendir (DIR, '.') ||
die "Unable to read current directory: ($!)\n";
my @files = grep {/\d+\.txt/} readdir (DIR);
closedir(DIR);
die "Multiple files found!\n" unless (scalar @files < 2);
my $newfile = $files[0] || 0;
$newfile =~ s/\.txt//;
$newfile++;
$newfile .= '.txt';
open(NEW, ">", $newfile) ||
die "Unable to create $newfile: ($!)\n";
close(NEW);
unlink($files[0]) if ($files[0]);
__________________________ ___
__________________________
#!/usr/bin/perl -w
use strict;
opendir (DIR, '.') ||
die "Unable to read current directory: ($!)\n";
my @files = grep {/\d+\.txt/} readdir (DIR);
closedir(DIR);
die "Multiple files found!\n" unless (scalar @files < 2);
my $newfile = $files[0] || 0;
$newfile =~ s/\.txt//;
$newfile++;
$newfile .= '.txt';
open(NEW, ">", $newfile) ||
die "Unable to create $newfile: ($!)\n";
close(NEW);
unlink($files[0]) if ($files[0]);
__________________________
Is there a database server accessible from all the machines that want to use the common counter?
You do not need perl to increment the number in the file. You can simply do it through the command line.. or a simple shell script....
cat test.txt | awk '{print $1+1}' > test.txt
This will take the number inside of the file "test.txt" and will increment it, and then write it back to the file "test.txt".
Is this what your looking for???
brado1234
cat test.txt | awk '{print $1+1}' > test.txt
This will take the number inside of the file "test.txt" and will increment it, and then write it back to the file "test.txt".
Is this what your looking for???
brado1234
ASKER
# my $d = CounterFile->new("$CONFIG:
# $d->inc;
when multiplt users hit the url at same time, i get the error
Software error:
Can't flock: No record locks available at /v/test.cgi line 40 and line 40 contains my $d = CounterFile->new("$CONFIG:
so i m creating my own counter logic
if u have any alternative solution i m open