You can avoid the second while loop which is just copying the content. Instead delete
File.txt and then rename
Temp.txt:
remove("File.txt");
rename("Temp.txt", "File.txt");
That will make your application run about half the time of your code with large files.