If Your buffer myfile is not shorter as 5 Bytes, this error can appears if You make
file.Close();
after You do not need the File more. It is a bug of CFile, I think, destructor of the CFile does not allways check if the File was closed and tries to close it itself. Try to remove Your file.Close() - function.