File input/output: Difference between revisions
m
→{{header|Standard ML}}
(Add lang example) |
|||
(12 intermediate revisions by 8 users not shown) | |||
Line 2,116:
w/o create-file throw r>
begin
pad
?dup while
pad swap 3 pick write-file throw
repeat
Line 2,126:
s" output.txt" s" input.txt" copy-file</syntaxhighlight>
Note the use of "2 pick" to get the input file handle and "3 pick" to get the output file handle. Local or global variables could have been used, but in this implementation simple stack manipulation was chosen. Also, only
Also, abort" can be used instead of throw if desired.
Line 2,559:
=={{header|Lang}}==
===Text-based===
{{libheader|lang-io-module}}
<syntaxhighlight lang="lang">
# Load the IO module
# Replace "<pathToIO.lm>" with the location where the io.lm
ln.loadModule(<pathToIO.lm>)
Line 2,570 ⟶ 2,571:
$text = [[io]]::fp.readFile($fileIn)
[[io]]::fp.writeFile($fileOut, $text)
[[io]]::fp.closeFile($fileIn)
[[io]]::fp.closeFile($fileOut)
</syntaxhighlight>
===Byte-based===
{{libheader|lang-io-module}}
<syntaxhighlight lang="lang">
# Load the IO module
# Replace "<pathToIO.lm>" with the location where the io.lm Lang module was installed to without "<" and ">"
ln.loadModule(<pathToIO.lm>)
$fileIn = [[io]]::fp.openFile(input.txt)
$fileOut = [[io]]::fp.openFile(output.txt)
$bytes = [[io]]::fp.readBytes($fileIn)
[[io]]::fp.writeBytes($fileOut, $bytes)
[[io]]::fp.closeFile($fileIn)
Line 2,921 ⟶ 2,939:
i.close()
o.close()</syntaxhighlight>
=={{header|Nu}}==
<syntaxhighlight lang="nu">
let text = open "input.txt"
$text | save "output.txt"
</syntaxhighlight>
=={{header|Objeck}}==
Line 3,507 ⟶ 3,531:
=={{header|Rust}}==
<syntaxhighlight lang="rust">
let contents = std::fs::read("input.txt").expect("error reading input.txt");
std::fs::write("output.txt", contents).expect("error writing output.txt");
}
</syntaxhighlight>
Line 3,547 ⟶ 3,565:
process::exit(code);
}</syntaxhighlight>
=={{header|S-BASIC}}==
Surprisingly, S-BASIC has no EOF function, so we have to rely on error trapping to signal end of file. S-BASIC stores its error codes at 103H, and we can position a base-located variable there to retrieve it. The result is something of a kludge, but it works. Other S-BASIC preculiarities are explained by the comments.
<syntaxhighlight lang="BASIC">
comment
Preserve file channel #0 (the console) while declaring channels
#2 and #3 as sequential-access ASCII files.
end
files d, sa, sa
var s = string:255
based errcode = integer
base errcode at 103H
comment
CP/M expects upper case file names, but S-BASIC does not
automatically convert file name arguments to upper case, so we
have to do that ourself.
end
create "OUTPUT.TXT"
open #1,"INPUT.TXT"
open #2,"OUTPUT.TXT"
comment
S-BASIC allows alphanumeric line "numbers" (which are treated simply
as labels) as the target of GOTO and GOSUB statements, but the first
character must be a digit
end
on error goto 9done
rem - runtime error code 15 signals read past end of file
while errcode <> 15 do
begin
input3 #1; s
print #2; s
end
9done
close #1
close #2
end
</syntaxhighlight>
=={{header|Scala}}==
Line 3,714 ⟶ 3,775:
=={{header|Standard ML}}==
===Reading the whole file at once as a string===
{{works with|SML/NJ|110.59}}
{{works with|Poly/ML|5.9.1}}
<syntaxhighlight lang="sml">
(* string -> string -> bool *)
fun copyFile from to =
let
val instream = TextIO.openIn from
Line 3,724 ⟶ 3,789:
in
true
end handle _ => false
===Binary mode using a buffer===
{{works with|Poly/ML|5.9.1}}
{{works with|SML/NJ|110.99.4}}
<syntaxhighlight lang="sml">
fun copyFile from to =
let
val instream = BinIO.openIn from
val outstream = BinIO.openOut to
fun aux () =
let
val buf = BinIO.inputN(instream, 1024)
in
if Word8Vector.length buf = 0
then ()
else (BinIO.output (outstream, buf); aux ())
end
in
(aux (); BinIO.closeIn instream; BinIO.closeOut outstream)
end
</syntaxhighlight>
=={{header|Stata}}==
Line 3,887 ⟶ 3,972:
fileio = ~command.files; &h.path.&h:= 'output.txt'!</syntaxhighlight>
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">|00 @System &vector $2 &expansion $2 &wst $1 &rst $1 &metadata $2 &r $2 &g $2 &b $2 &debug $1 &state $1
|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
|a0 @File1 &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2
|b0 @File2 &vector $2 &success $2 &stat $2 &delete $1 &append $1 &name $2 &length $2 &read $2 &write $2
|0100
;in-file .File1/name DEO2
;out-file .File2/name DEO2
&loop
#0100 .File1/length DEO2k POP
;buffer .File1/read DEO2
.File1/success DEI2
.File2/length DEO2k POP
;buffer .File2/write DEO2
EQU2 ?&loop
#80 .System/state DEO
BRK
@in-file "input.txt 00
@out-file "output.txt 00
@buffer $100</syntaxhighlight>
=={{header|Vedit macro language}}==
Line 3,902 ⟶ 4,014:
=={{header|Wren}}==
<syntaxhighlight lang="
var contents = File.read("input.txt")
Line 3,944 ⟶ 4,056:
=={{header|Zig}}==
'''Works with:''' 0.10.x, 0.11.x, 0.12.0-dev.1357+10d03acdb
<syntaxhighlight lang="zig">const std = @import("std");
pub fn main() (error{OutOfMemory} || std.fs.File.OpenError || std.fs.File.ReadError || std.fs.File.WriteError)!void {
var
defer
const allocator = gpa.allocator();
var input_file = try cwd.openFile("input.txt", .{ .mode = .read_only });
var
defer output_file.close();
// Restrict input_file's size to "up to 10 MiB".
var input_file_content = try input_file.readToEndAlloc(allocator, 10 * 1024 * 1024);
defer allocator.free(input_file_content);
try output_file.writeAll(input_file_content);
return;
}</syntaxhighlight>
|