abhishek_akj has asked for the wisdom of the Perl Monks concerning the following question:

I seem to be running into an issue while using truncate.

I am using it as

truncate FILEH, "$file_cut_length" or print "Unable : $! \n";
I am getting the error:
Unable : Invalid argument
The value of $file_cut_length comes out to be 7782756853

Is that an issue here?
If yes, how to handle such large files then?

Replies are listed 'Best First'.
Re: Problem truncating a huge file
by tilly (Archbishop) on Aug 27, 2009 at 00:25 UTC
    There are several possible issues. Try creating a smaller file (eg 500 MB) and truncate it to something smaller still (eg 300 MB). If that works then you've got a 32-bit issue.

    Your next step should be to run the following program:

    use Config; print "$Config{uselargefiles\n";
    If that says that uselargefiles should be defined, then you've found a Perl bug and should file a perlbug with explanation and sample code to produce it. (I would suggest having sample code that writes the file that you then fail to truncate.)

    If uselargefiles is not defined then it would be best to install a version of Perl that can handle large files. If you want a workaround you should rename the large file to something else, and then do a read/write loop to write a copy that stops at the desired size. Then delete the original. Be warned that this utility will not be as fast as the native function.

      I dont think size of the file is an issue over here, as the same script is reading and writing to the file perfectly. It is just truncate which throws me this error.
        The file is large enough that 32-bit interfaces are going to have trouble. When you call truncate you're hitting a different C function than normal reading/writing hits, and it is seldom used. Is it really beyond the bounds of possibility that this case got missed?

        Given that it is easy to test it is at least worth testing the possibility, even if you think the theory is unlikely.

Re: Problem truncating a huge file
by Mr. Muskrat (Canon) on Aug 26, 2009 at 17:27 UTC

    Are you sure that you want to truncate the file to 778275853 bytes?

    What size is the file that you are trying to truncate? Is it greater than 7782756853 bytes?

    What OS?

    Did you use the following?
    use Fnctl ':flock';
    If so, you might try:
    use Fnctl;

    Update: I seem to have left out the most important question... What is the actual issue that you are having?

      Yes , the file is larger than 7782756853 bytes. OS: FreeBSD The actual issue is that I want to remove the last line of a very large file, thats all. For that I used File::ReadBackwards, got the pointer position for the start of last line, and wanted to truncate from that. If so, you might try: use Fnctl; How would this help?

        I have seen reports of truncate failing when use Fnctl ':flock' was used but that changing it to use Fnctl; allowed it succeed. *shrug*

Re: Problem truncating a huge file
by zwon (Abbot) on Aug 26, 2009 at 18:24 UTC

    File that you're truncating should be opened for writing.

      The file is opened in Read-Write Mode, and I am able to successfully write to the file as well, it is truncate which is giving me error.