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

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

by merlyn (Sage)
on Jan 03, 2001 at 22:17 UTC ( #49581=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > How do I pre-extend a file to a specified size - Answer contributed by merlyn

seek HANDLE, 0, $desired_size -1; print HANDLE "\0"; seek HANDLE, 0, 0;

Replies are listed 'Best First'.
Re: Answer: How do I pre-extend a file to a specified size
by Anonymous Monk on Jan 05, 2001 at 06:17 UTC
    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;
        Thanks... that did the trick (kind-a): Now works fine with Win9x/2K but fails to extend the file on OpenVMS. I'll forward a bug report to the VMS porting people.
Re: Answer: How do I pre-extend a file to a specified size
by chipmunk (Parson) on Jan 04, 2001 at 07:29 UTC
    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.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2020-04-06 18:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (40 votes). Check out past polls.

    Notices?