in reply to File I/O

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