• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1043
  • Last Modified:

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

0
yuk99
Asked:
yuk99
1 Solution
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now