File input/output: Difference between revisions
m
→{{header|Standard ML}}
(Dialects of BASIC moved to the BASIC section.) |
|||
(17 intermediate revisions by 11 users not shown) | |||
Line 793:
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
This is only meant to copy a sequential text file. It is very unlikely that this works copying a random access text file.
Line 1,037 ⟶ 1,026:
==={{header|QBasic}}===
See [[#QuickBASIC|QuickBASIC]].
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">
' File input/output
OPEN "INPUT.TXT" FOR INPUT AS #1
OPEN "OUTPUT.TXT" FOR OUTPUT AS #2
DO UNTIL EOF(1)
LOOP
CLOSE #1
CLOSE #2
END
</syntaxhighlight>
==={{header|RapidQ}}===
Line 2,119 ⟶ 2,116:
w/o create-file throw r>
begin
pad
?dup while
pad swap 3 pick write-file throw
repeat
Line 2,129 ⟶ 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,560 ⟶ 2,557:
{{VI snippet}}<br/>
[[File:LabVIEW File IO.png]]
=={{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 Lang module was installed to without "<" and ">"
ln.loadModule(<pathToIO.lm>)
$fileIn = [[io]]::fp.openFile(input.txt)
$fileOut = [[io]]::fp.openFile(output.txt)
$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)
[[io]]::fp.closeFile($fileOut)
</syntaxhighlight>
=={{header|Lang5}}==
Line 2,907 ⟶ 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,493 ⟶ 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,533 ⟶ 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,654 ⟶ 3,729:
while output = input :s(while)
end</syntaxhighlight>
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "filecopy" )
@( description, "The job is to create a file called 'output.txt', and place in it" )
@( description, "the contents of the file 'input.txt'." )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/File_IO" );
pragma software_model( nonstandard );
pragma license( unrestricted );
procedure filecopy is
begin
if not files.is_readable( "input.txt" ) then
put_line( standard_error, source_info.source_location & ": input file is not readable" );
command_line.set_exit_status( 192 );
return;
end if;
-- With standard shell commands
cp input.txt output.txt;
-- Using built-in capabilities
pragma restriction( no_external_commands );
declare
inputfile : file_type;
outputfile : file_type;
begin
create( outputfile, out_file, "output.txt" );
open( inputfile, in_file, "input.txt" );
while not end_of_file( inputfile ) loop
put_line( outputfile, get_line( inputfile ) );
end loop;
close( inputfile ) @ ( outputfile );
end;
end filecopy;</syntaxhighlight>
=={{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,666 ⟶ 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,829 ⟶ 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,844 ⟶ 4,014:
=={{header|Wren}}==
<syntaxhighlight lang="
var contents = File.read("input.txt")
Line 3,886 ⟶ 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>
{{omit from|EasyLang}}
{{omit from|HTML}}
{{omit from|Order}}
|