Take a look at posix_fallocate()
:
NAME
posix_fallocate - allocate file space
SYNOPSIS
int posix_fallocate(int fd, off_t offset, off_t len);
DESCRIPTION
The function posix_fallocate() ensures that disk space is allocated for
the file referred to by the descriptor fd for the bytes in the range
starting at offset and continuing for len bytes. After a successful
call to posix_fallocate(), subsequent writes to bytes in the specified
range are guaranteed not to fail because of lack of disk space.
edit In the comments you indicate that you use C++ streams to write to the file. As far as I know, there's no standard way to get the file descriptor (fd
) from a std::fstream
.
With this in mind, I would make disk space pre-allocation a separate step in the process. It would:
open()
the file;
- use
posix_fallocate()
;
close()
the file.
This can be turned into a short function to be called before you even open the fstream
.