fixed the function to write a txt fil