[Gambas-user] fastest way to "zero-out" a huge binary file

Benoît Minisini gambas at ...1...
Sun Jan 24 00:36:54 CET 2010


> I need to create an 8 gigabyte binary file with zero values throughout it.
>  Is there a faster way to do this than to create a big string of zeros and
>  write it to the file multiple times? What I'm doing right now works but
>  seems like a really ugly method:
> 
> ' Zero-out the file.
> Zero = Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0)
>  & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) &
>  Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) &
>  Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0) &
>  Chr$(0) & Chr$(0) & Chr$(0) & Chr$(0)
> FOR counter = 1 TO 65536
>   Zeros = Zeros & Zero
> NEXT
> FOR counter = 1 TO 4096
>   WRITE #Elevation, Zeros
> NEXT
> 
> 
> -----
> Kevin Fishburne, Eight Virtues
> www:  http://sales.eightvirtues.com http://sales.eightvirtues.com
> e-mail:  mailto:sales at ...1887... sales at ...1887...
> phone: (770) 853-6271
> 


On Linux, If you seek and write past the real end of a file, then the file is 
automatically extended.

So the simplest is writing where you want in the file only when you need. Just 
don't do that randomly, to prevent the disk from seeking too much.

Most Linux file systems will reserve only pieces where something was written. 
The other part of the file that are only zeros are virtual, and will not 
consume your hard disk.

Anyway, why do you need 8Gb of data at once?

-- 
Benoît Minisini




More information about the User mailing list