Reputation: 857
I have essentially the following code:
int fileWrite(int file, void * pBuffer, size_t size)
{
size_t bytesWritten = (size_t)write( file, pBuffer, size ) ;
if (bytesWritten != size)
{
return -1;
}
return 0;
}
It works if the size is 1GB, but when the size is ~2GB, it get 4K bytes left consistently. I can fix this by wrapping write in a loop and moving the buffer up but I'm curious as to why it is always failing.
For example if size is 2147483648, write only writes 2147479552, leaving 4096 unwritten. Why would this happen and is it correct to always wrap write in a loop?
Upvotes: 34
Views: 1701
Reputation: 18864
You can find the answer in man 2 write
:
It is not an error if this number is smaller than the number of bytes requested; this may happen for example because the disk device was filled.
And from the write()
man page description:
ssize_t write(int fd, const void *buf, size_t count);
According to POSIX.1, if
count
is greater thanSSIZE_MAX
, the result is implementation-defined; see NOTES for the upper limit on Linux.NOTES
On Linux,
write()
(and similar system calls) will transfer at most0x7ffff000
(2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.)
Upvotes: 53