File input/output: Difference between revisions

m
(Add Uxntal)
(4 intermediate revisions by 4 users not shown)
Line 3,531:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">usefn std::fs::File;main() {
let contents = std::fs::read("input.txt").expect("error reading input.txt");
use std::io::{Read, Write};
std::fs::write("output.txt", contents).expect("error writing output.txt");
 
fn main() {
let mut file = File::open("input.txt").unwrap();
let mut data = Vec::new();
file.read_to_end(&mut data).unwrap();
let mut file = File::create("output.txt").unwrap();
file.write_all(&data).unwrap();
}
</syntaxhighlight>
Line 3,571 ⟶ 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,738 ⟶ 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">fun copyFile (from, to) =
(* string -> string -> bool *)
fun copyFile from to =
let
val instream = TextIO.openIn from
Line 3,748 ⟶ 3,789:
in
true
end handle _ => false;</syntaxhighlight>
===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,913 ⟶ 3,974:
 
=={{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
|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
Line 3,938 ⟶ 3,998:
@in-file "input.txt 00
@out-file "output.txt 00
@buffer $100</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Vedit macro language}}==
Line 3,955 ⟶ 4,014:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
var contents = File.read("input.txt")
23

edits