in reply to Re: How do I pre-extend a file to a specified size
in thread How do I pre-extend a file to a specified size

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.

Replies are listed 'Best First'.
Re: Re: Answer: How do I pre-extend a file to a specified size
by chipmunk (Parson) on Jan 05, 2001 at 07:11 UTC
    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.

        That snippet doesn't check any return values so it is possible that the seek() failed. But I really suspect that VMS creates a sparse file and reports the amount of space the file is using, not the maximum byte offset for the file.

        The same thing often happens under Unix -- the file isn't really preextended as far as allocating that much space on the disk. It is just that "ls" reports the maximum byte offset for the file, not how much disk space has been allocated to it.

                - tye (but my friends call me "Tye")