in reply to File I/O

try to close file and open for writing, you opened it for reading only in your example