Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file counter?

Posted on 2000-04-03
6
Medium Priority
?
207 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 80 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 85

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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

972 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