Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
Why does write continuously leave 4K bytes in the buffer?

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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 than SSIZE_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 most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda