how to detect end of file while using UNIX I/O
Check out the man pages of the read() and write() syscalls for *nix.