Fixed fwrite() error behaviour.

Closes issue #1909.