Task

(Over)write a file so that it contains a string.



The reverse of Read entire file—for when you want to update or create a file which you would read in its entirety all at once.




program overwriteFile(input, output, stdErr);
{$mode objFPC} // for exception treatment
uses
	sysUtils; // for applicationName, getTempDir, getTempFileName
	// also: importing sysUtils converts all run-time errors to exceptions
resourcestring
	hooray = 'Hello world!';
var
	FD: text;
begin
	// on a Debian GNU/Linux distribution,
	// this will write to /tmp/overwriteFile00000.tmp (or alike)
	assign(FD, getTempFileName(getTempDir(false), applicationName()));
	try
		rewrite(FD); // could fail, if user has no permission to write
		writeLn(FD, hooray);
	finally
		close(FD);
	end;
end.