Search code examples
clinuxmemorymemory-managementfwrite

fwrite not return 0 when harddisk is full in linux why?


when hard disk is 100% full, fwrite(say 1000 Bytes) return 0 [Fail , as expected] but when hard disk is has little empty space say 600Bytes , then fwrite (1000 bytes) not return 0 [fail] but return say 300 Bytes , again calling fwrite still return 300bytes, fwrite never fails , even if we call 100 times ?

The Error no is setting properly to 28. My question is Why is behaviour of fwrite ? is this right? if fwrite return less than bytes which we want to write then this means disk is full ?

anh suggestion to handle this situation ?


Solution

  • POSIX says this about fwrite:

    If an error occurs, the resulting value of the file-position indicator for the stream is unspecified.

    This means that repeating that same fwrite on the same stream is an error in your code - you don't know where the stream is positioned, so attempting to write to it is not a good idea at all.

    You handle this like it seems you're doing already: check errno after a short fwrite and do whatever is appropriate for your application.