Solved

file counter?

Posted on 2000-04-03
6
194 Views
Last Modified: 2013-12-25
how to make a file (exclusive)file counter?
0
Comment
Question by:syu1
  • 3
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 20 total points
ID: 2681557
Here's how you increment a number in a file, using flock:

use Fcntl qw(:DEFAULT :flock);

sysopen(FH, "numfile", O_RDWR|O_CREAT)                                    or die "can't open numfile: $!";
flock(FH, LOCK_EX) or die "can't write-lock numfile: $!";

# Now we have acquired the lock, it's safe for I/O
$num = <FH> || 0;                   # DO NOT USE "or" THERE!!
seek(FH, 0, 0)                      or die "can't rewind numfile : $!";
truncate(FH, 0)                     or die "can't truncate numfile: $!";
print FH $num+1, "\n"               or die "can't write numfile: $!";
close(FH)                           or die "can't close numfile: $!";

Closing the filehandle flushes the buffers and unlocks the file.
===========================================
NOTE: truncate EXPR, LENGTH

This function truncates the file opened on FILEHANDLE, or named by EXPR, to the specified length.
0
 

Author Comment

by:syu1
ID: 2691940
thanks!

your answer is really work, but what is
 
truncate(FH, 0)                     or die "can't truncate numfile: $!";


really work?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2692004
perldoc -f truncate
=item truncate FILEHANDLE,LENGTH

=item truncate EXPR,LENGTH

Truncates the file opened on FILEHANDLE, or named by EXPR, to the
specified length.  Produces a fatal error if truncate isn't implemented
on your system.  Returns TRUE if successful, the undefined value
otherwise.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:maneshr
ID: 2693572
Syu1,
glad to know that the solution worked for you.


Also here is a brief note about truncate

truncate FILEHANDLE, LENGTH
truncate EXPR, LENGTH

This function truncates the file opened on FILEHANDLE, or named by EXPR, to the specified length. The function produces a fatal error if truncate(2) or an equivalent isn't implemented on your system. (You can always truncate a file by copying the front of it, if you have the disk space.)


truncate(FH, 0)                     or die "can't truncate numfile: $!";

what the above means is if truncate fails then show the Error message "can't truncate numfile: <actual error message" and exit the Perl program with a nonzero exit status.

Hope that helps
0
 

Author Comment

by:syu1
ID: 2697067
thanks lot! maneshr, I really appreciate it!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2698620
youre welcome :-)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question