File input/output: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
m (Make the Groovy example fulfill the task by actually using an intermediate variable)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 16:
<lang 11l>V file_contents = File(‘input.txt’).read()
File(‘output.txt’, ‘w’).write(file_contents)</lang>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
Line 171 ⟶ 172:
</lang>
 
=={{header|ACL2}}==
<lang lisp>:set-state-ok t
Line 1,407 ⟶ 1,409:
 
end</lang>
 
=={{header|Elena}}==
ELENA 4.x :
Line 1,439 ⟶ 1,442:
'''Built in function:'''
<lang Elixir>File.cp!("input.txt", "output.txt")</lang>
 
=={{header|Erlang}}==
 
<lang erlang>
-module( file_io ).
 
-export( [task/0] ).
 
task() ->
{ok, Contents} = file:read_file( "input.txt" ),
ok = file:write_file( "output.txt", Contents ).
</lang>
 
=={{header|Euphoria}}==
Line 1,464 ⟶ 1,479:
close(out)
close(in)</lang>
 
=={{header|Erlang}}==
 
<lang erlang>
-module( file_io ).
 
-export( [task/0] ).
 
task() ->
{ok, Contents} = file:read_file( "input.txt" ),
ok = file:write_file( "output.txt", Contents ).
</lang>
 
=={{header|F Sharp|F#}}==
Line 1,799 ⟶ 1,802:
; that's all
close,/all</lang>
 
 
=={{header|Io}}==
Line 2,115 ⟶ 2,117:
 
copy "input.txt "output.txt</lang>
 
=={{header|Lua}}==
<lang lua>
Line 2,177 ⟶ 2,180:
Using_Buffer
</lang>
 
=={{header|MAXScript}}==
<lang maxscript>inFile = openFile "input.txt"
outFile = createFile "output.txt"
while not EOF inFile do
(
format "%" (readLine inFile) to:outFile
)
close inFile
close outFile</lang>
 
 
=={{header|Maple}}==
Line 2,202 ⟶ 2,194:
If[FileExistsQ["output.txt"], DeleteFile["output.txt"], Print["No output yet"] ];
CopyFile["input.txt", "output.txt"]</lang>
 
=={{header|MAXScript}}==
<lang maxscript>inFile = openFile "input.txt"
outFile = createFile "output.txt"
while not EOF inFile do
(
format "%" (readLine inFile) to:outFile
)
close inFile
close outFile</lang>
 
=={{header|Mercury}}==
Line 2,389 ⟶ 2,391:
}
}</lang>
 
=={{header|Objective-C}}==
 
For copying files, using <code>NSFileManager</code> is preferred:
 
<lang objc>[[NSFileManager defaultManager] copyItemAtPath:@"input.txt" toPath:@"output.txt" error:NULL];</lang>
 
If you want to do it manually:
 
<lang objc>NSData *data = [NSData dataWithContentsOfFile:@"input.txt"];
 
[data writeToFile:@"output.txt" atomically:YES];</lang>
 
Displayed without error checking to make it more clear. In real code you will need to add lot of error checking code, and maybe use <tt>dataWithContentsOfFile:error:</tt> if you want to get error information on failure. However, this code will mostly work correctly even if input does not exist or is not accessible. <tt>dataWithContentsOfFile:</tt> will return nil, and sending nil the message <tt>writeTofile:atomically:</tt> does nothing :-)
 
The second argument (<tt>atomically:YES</tt>) write the content to a temporary file, and rename the temporary file to the destination file, replacing existing file.
 
=={{header|Object Pascal}}==
Line 2,421 ⟶ 2,407:
end;
end;</lang>
 
=={{header|Objective-C}}==
 
For copying files, using <code>NSFileManager</code> is preferred:
 
<lang objc>[[NSFileManager defaultManager] copyItemAtPath:@"input.txt" toPath:@"output.txt" error:NULL];</lang>
 
If you want to do it manually:
 
<lang objc>NSData *data = [NSData dataWithContentsOfFile:@"input.txt"];
 
[data writeToFile:@"output.txt" atomically:YES];</lang>
 
Displayed without error checking to make it more clear. In real code you will need to add lot of error checking code, and maybe use <tt>dataWithContentsOfFile:error:</tt> if you want to get error information on failure. However, this code will mostly work correctly even if input does not exist or is not accessible. <tt>dataWithContentsOfFile:</tt> will return nil, and sending nil the message <tt>writeTofile:atomically:</tt> does nothing :-)
 
The second argument (<tt>atomically:YES</tt>) write the content to a temporary file, and rename the temporary file to the destination file, replacing existing file.
 
=={{header|OCaml}}==
Line 2,551 ⟶ 2,553:
 
Perl has also a powerful mechanism in conjunction with opening files called IO disciplines. It allows you to automatically apply chainable transformations on the input and output. Mangling newlines, gzip (de)compression and character encoding are the most used examples.
 
=={{header|Perl 6}}==
 
If it is okay to have a temporary copy of the entire file in memory:
 
{{works with|Rakudo|2016.07}}
<lang perl6>spurt "output.txt", slurp "input.txt";</lang>
 
Otherwise, copying line-by line:
 
{{works with|Rakudo|2015.12}}
<lang perl6>my $in = open "input.txt";
my $out = open "output.txt", :w;
for $in.lines -> $line {
$out.say: $line;
}
$in.close;
$out.close;</lang>
 
=={{header|Phix}}==
Line 2,826 ⟶ 2,810:
(lambda ()
(write file-content)))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
If it is okay to have a temporary copy of the entire file in memory:
 
{{works with|Rakudo|2016.07}}
<lang perl6>spurt "output.txt", slurp "input.txt";</lang>
 
Otherwise, copying line-by line:
 
{{works with|Rakudo|2015.12}}
<lang perl6>my $in = open "input.txt";
my $out = open "output.txt", :w;
for $in.lines -> $line {
$out.say: $line;
}
$in.close;
$out.close;</lang>
 
=={{header|RapidQ}}==
Line 2,867 ⟶ 2,870:
=={{header|Raven}}==
<lang raven>'input.txt' read 'output.txt' write</lang>
 
=={{header|REALbasic}}==
<lang vb>
Line 3,118 ⟶ 3,122:
(File newNamed: 'output.txt' &mode: File CreateWrite) sessionDo: [| :out |
in >> out]]</lang>
 
=={{header|Smalltalk}}==
<lang smalltalk>| in out |
in := FileStream open: 'input.txt' mode: FileStream read.
out := FileStream open: 'output.txt' mode: FileStream write.
[ in atEnd ]
whileFalse: [
out nextPut: (in next)
]</lang>
 
=={{header|Snabel}}==
Line 3,142 ⟶ 3,155:
@q +? {@w &_ for} when
</lang>
 
=={{header|Smalltalk}}==
<lang smalltalk>| in out |
in := FileStream open: 'input.txt' mode: FileStream read.
out := FileStream open: 'output.txt' mode: FileStream write.
[ in atEnd ]
whileFalse: [
out nextPut: (in next)
]</lang>
 
=={{header|SNOBOL4}}==
Line 3,278 ⟶ 3,282:
status=WRITE(path2output,contentinput)
</lang>
 
=={{header|TXR}}==
 
10,333

edits