Fixed fwrite() error behaviour.

Closes #1909.