Cannot read from and write to the same file