How to update a text file dynamically