Solved

file counter?

Posted on 2000-04-03
6
189 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validate form text box with javascript. Numeric and not blank. 6 39
disabled AD accounts info 3 69
Edit Registry Key via Batch File 8 55
Ready Made PHP Scripts 5 62
Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now