Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

file counter?

Posted on 2000-04-03
6
Medium Priority
?
205 Views
Last Modified: 2013-12-25
how to make a file (exclusive)file counter?
0
Comment
Question by:syu1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to count occurrences of each item in an array.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

722 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