in reply to conveting .txt file to .dat

The code you've shown cannot be the code responsible for that, because the code you've shown does overwrite the input file and does not mention .dat at all.