Use exceptions; Use operator>> and operator<< instead of read() and write.