Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I pre-extend a file to a specified size

by Anonymous Monk
on Jan 03, 2001 at 22:16 UTC ( #49580=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (files)

Some operating systems can optimize file creation (especially for fixed binary record type files) if the file is pre-extended to desired size. This eliminates multiple file extension requests and can produce less fragmented files systems on multi-user systems.

Any tricks to indicate desired size of file during open?

Originally posted as a Categorized Question.

  • Comment on How do I pre-extend a file to a specified size

Replies are listed 'Best First'.
Re: How do I pre-extend a file to a specified size
by merlyn (Sage) on Jan 03, 2001 at 22:17 UTC
    seek HANDLE, 0, $desired_size -1; print HANDLE "\0"; seek HANDLE, 0, 0;
      I tried the following code segment on both Win98 and VMS and it failed to give the desired result:
      open HANDLE, ">temp.tmp" || die; $desired_size=1000000; seek HANDLE, 0, $desired_size -1; print HANDLE "\0"; seek HANDLE, 0, 0; close HANDLE;
      The resulting file on Win98 is one byte long!! On VMS it is one block (minimum file size). It would appear that the seek doesn't extend the file past its current size (initially 0 bytes). Any suggestion appreciated.
        It is rather frustrating that the Q&A section is not properly threaded. I pointed out the bug in merlyn's code in Re: Answer: How do I pre-extend a file to a specified size, but looking at the node for the question, there's no indication that merlyn's answer even has a response. Meanwhile, looking at the node for merlyn's answer, there is no way to get back to the original question.

        To fix the bug, swap the second and third arguments to seek:

        seek HANDLE, $desired_size -1, 0;
      That should be: seek HANDLE, $desired_size -1, 0; The second argument to seek is the number of bytes to move the position by, while the third argument is a number indicating where to start. 0 seeks from the beginning of the file, 1 seeks from the current location in the file, and 2 seeks from the end of the file.
Re: How do I pre-extend a file to a specified size
by abhishek_akj (Initiate) on Aug 26, 2009 at 10:24 UTC
    open FILEHANDLE, " +< filename" or die "Unable to open: $! \n"; truncate FILEHANDLE, $desired_length or print "Unable to truncate: $ +! \n";
    truncate just sets the filesize of the file opened by handle to whatever you specify; you can use it to remove characters from file as well as increase the filesize.

      Actually, setting the length greater than the actual file size is undefined behavior.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://49580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2021-05-14 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (149 votes). Check out past polls.

    Notices?