Perl truncate function with files >4GB

I'm trying to truncate a very large file with simple Perl script below. It works fine with files less than 4GB, but returns "Invalid argument" error with larger files. I cannot find any limit for truncate function in perldoc or with google.

I'm running Active Perl 5.10 on Windows XP SP2, 4GB ram, NTFS file system.

my $filename = "c:/temp/test.dat";
# the test.dat file size is 4585291776
my $length = 4000000000;
truncate $filename, $length
    or die "Couldn't truncate: $!\n";

Open in new window

LVL 15
yuk99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

FishMongerCommented:
In the code you posted $filename is simply a string.  The truncate function expects a filehandle, not a string.
0
yuk99Author Commented:
Nope, it can be a file name. And as I wrote it works on files less than 4GB.
0
yuk99Author Commented:
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Adam314Commented:
Have you tried it on a filehandle instead?
my $filename = "c:/temp/test.dat";
# the test.dat file size is 4585291776
open(my $in, '<', $filename) or die "Couldn't open: $!\n";
my $length = 4000000000;
truncate $in, $length
    or die "Couldn't truncate: $!\n";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yuk99Author Commented:
@Adam314:
Yes, the filehandle works. Do you know why truncate behaves like this? The answer is accepted. Thanks a lot.

@dda:
I've tried perl -V, and it returns "uselargefiles=define".
0
yuk99Author Commented:
Adam, you saved me again. Thanks for your help!
0
yuk99Author Commented:
@Adam312:
I only had to change '<' to '+<' to allow the file to be updated. Or I get 'Permission denied' error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.