Cache file read/write refactoring

Use buffered read/write functions instead of low-level system calls.