I need to create a file of precisely x bits (which will then be
I thought using
ftruncate() was sufficient, until I saw code from various sources (ex. Mozilla) using
F_PREALLOCATE first, then used
From the doc's, I can't ascertain why one would do this, and if it is necessary.
Best How To :
No it is not necessary. If you use
ftruncate it is possible that the file is virtually constructed, means that blocks are reserved in the total allocated blocks count but not really allocated to the file (files can have holes relatively to their allocated blocks). In that case, block allocation is deferred until read or write on it.
If you use
F_PREALLOCATE then the allocation is not deferred.
I'm not sure this will makes any real difference; may be in time but I can't really see why. It may be a fragmentation concern.