Write entire file: Difference between revisions

add →‎Free Pascal: implementation
(Added Powershell implementation)
(add →‎Free Pascal: implementation)
Line 335:
Print #1, "This is a string"
Close #1</lang>
 
=={{header|Free Pascal}}==
''See also: [[#Pascal]]''
<lang Pascal>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.</lang>
 
=={{header|Gambas}}==
Line 479 ⟶ 502:
let s = String.init 26 (fun i -> char_of_int (i + int_of_char 'A')) in
write_file filename s</lang>
 
=={{header|Pascal}}==
''See also: [[#Free Pascal|Free Pascal]]''
 
In Standard Pascal, a list of identifiers in the program header sets up a list of file handlers.
<lang Pascal>{$ifDef FPC}{$mode ISO}{$endIf}
program overwriteFile(FD);
begin
writeLn(FD, 'Whasup?');
close(FD);
end.</lang>
In some environments, it is the caller’s sole responsibility to invoke the program properly.
I. e., the program above could be invoked on a Bourne-again shell in this fashion:
<lang bash>./overwriteFile >&- <&- 0>/tmp/foo # open file descriptor with index 0 for writing</lang>
 
=={{header|Perl}}==
149

edits