Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
3
Task/File-input-output/00DESCRIPTION
Normal file
3
Task/File-input-output/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{{selection|Short Circuit|Console Program Basics}}
|
||||
|
||||
In this task, the job is to create a file called "output.txt", and place in it the contents of the file "input.txt", ''via an intermediate variable.'' In other words, your program will demonstrate: (1) how to read from a file into a variable, and (2) how to write a variable's contents into a file. Oneliners that skip the intermediate variable are of secondary interest — operating systems have copy commands for that.
|
||||
2
Task/File-input-output/00META.yaml
Normal file
2
Task/File-input-output/00META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: File handling
|
||||
39
Task/File-input-output/ACL2/file-input-output.acl2
Normal file
39
Task/File-input-output/ACL2/file-input-output.acl2
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
:set-state-ok t
|
||||
|
||||
(defun read-channel (channel limit state)
|
||||
(mv-let (ch state)
|
||||
(read-char$ channel state)
|
||||
(if (or (null ch)
|
||||
(zp limit))
|
||||
(let ((state (close-input-channel channel state)))
|
||||
(mv nil state))
|
||||
(mv-let (so-far state)
|
||||
(read-channel channel (1- limit) state)
|
||||
(mv (cons ch so-far) state)))))
|
||||
|
||||
(defun read-from-file (filename limit state)
|
||||
(mv-let (channel state)
|
||||
(open-input-channel filename :character state)
|
||||
(mv-let (contents state)
|
||||
(read-channel channel limit state)
|
||||
(mv (coerce contents 'string) state))))
|
||||
|
||||
(defun write-channel (channel cs state)
|
||||
(if (endp cs)
|
||||
(close-output-channel channel state)
|
||||
(let ((state (write-byte$ (char-code (first cs))
|
||||
channel state)))
|
||||
(let ((state (write-channel channel
|
||||
(rest cs)
|
||||
state)))
|
||||
state))))
|
||||
|
||||
(defun write-to-file (filename str state)
|
||||
(mv-let (channel state)
|
||||
(open-output-channel filename :byte state)
|
||||
(write-channel channel (coerce str 'list) state)))
|
||||
|
||||
(defun copy-file (in out state)
|
||||
(mv-let (contents state)
|
||||
(read-from-file in (expt 2 40) state)
|
||||
(write-to-file out contents state)))
|
||||
46
Task/File-input-output/ALGOL-68/file-input-output.alg
Normal file
46
Task/File-input-output/ALGOL-68/file-input-output.alg
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
PROC copy file v1 = (STRING in name, out name)VOID: (
|
||||
# note: algol68toc-1.18 - can compile, but not run v1 #
|
||||
INT errno;
|
||||
FILE in file, out file;
|
||||
errno := open(in file, in name, stand in channel);
|
||||
errno := open(out file, out name, stand out channel);
|
||||
|
||||
BOOL in ended := FALSE;
|
||||
PROC call back ended = (REF FILE f) BOOL: in ended := TRUE;
|
||||
on logical file end(in file, call back ended);
|
||||
|
||||
STRING line;
|
||||
WHILE
|
||||
get(in file, (line, new line));
|
||||
# WHILE # NOT in ended DO # break to avoid excess new line #
|
||||
put(out file, (line, new line))
|
||||
OD;
|
||||
ended:
|
||||
close(in file);
|
||||
close(out file)
|
||||
);
|
||||
|
||||
PROC copy file v2 = (STRING in name, out name)VOID: (
|
||||
INT errno;
|
||||
FILE in file, out file;
|
||||
errno := open(in file, in name, stand in channel);
|
||||
errno := open(out file, out name, stand out channel);
|
||||
|
||||
PROC call back ended = (REF FILE f) BOOL: GO TO done;
|
||||
on logical file end(in file, call back ended);
|
||||
|
||||
STRING line;
|
||||
DO
|
||||
get(in file, line);
|
||||
put(out file, line);
|
||||
get(in file, new line);
|
||||
put(out file, new line)
|
||||
OD;
|
||||
done:
|
||||
close(in file);
|
||||
close(out file)
|
||||
);
|
||||
|
||||
test:(
|
||||
copy file v2("input.txt","output.txt")
|
||||
)
|
||||
5
Task/File-input-output/AWK/file-input-output.awk
Normal file
5
Task/File-input-output/AWK/file-input-output.awk
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
BEGIN {
|
||||
while ( (getline <"input.txt") > 0 ) {
|
||||
print >"output.txt"
|
||||
}
|
||||
}
|
||||
30
Task/File-input-output/Ada/file-input-output-1.ada
Normal file
30
Task/File-input-output/Ada/file-input-output-1.ada
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Read_And_Write_File_Line_By_Line is
|
||||
Input, Output : File_Type;
|
||||
begin
|
||||
Open (File => Input,
|
||||
Mode => In_File,
|
||||
Name => "input.txt");
|
||||
Create (File => Output,
|
||||
Mode => Out_File,
|
||||
Name => "output.txt");
|
||||
loop
|
||||
declare
|
||||
Line : String := Get_Line (Input);
|
||||
begin
|
||||
-- You can process the contents of Line here.
|
||||
Put_Line (Output, Line);
|
||||
end;
|
||||
end loop;
|
||||
Close (Input);
|
||||
Close (Output);
|
||||
exception
|
||||
when End_Error =>
|
||||
if Is_Open(Input) then
|
||||
Close (Input);
|
||||
end if;
|
||||
if Is_Open(Output) then
|
||||
Close (Output);
|
||||
end if;
|
||||
end Read_And_Write_File_Line_By_Line;
|
||||
51
Task/File-input-output/Ada/file-input-output-2.ada
Normal file
51
Task/File-input-output/Ada/file-input-output-2.ada
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
with Ada.Command_Line, Ada.Text_IO; use Ada.Command_Line, Ada.Text_IO;
|
||||
|
||||
procedure Read_And_Write_File_Line_By_Line is
|
||||
Read_From : constant String := "input.txt";
|
||||
Write_To : constant String := "output.txt";
|
||||
|
||||
Input, Output : File_Type;
|
||||
begin
|
||||
begin
|
||||
Open (File => Input,
|
||||
Mode => In_File,
|
||||
Name => Read_From);
|
||||
exception
|
||||
when others =>
|
||||
Put_Line (Standard_Error,
|
||||
"Can not open the file '" & Read_From & "'. Does it exist?");
|
||||
Set_Exit_Status (Failure);
|
||||
return;
|
||||
end;
|
||||
|
||||
begin
|
||||
Create (File => Output,
|
||||
Mode => Out_File,
|
||||
Name => Write_To);
|
||||
exception
|
||||
when others =>
|
||||
Put_Line (Standard_Error,
|
||||
"Can not create a file named '" & Write_To & "'.");
|
||||
Set_Exit_Status (Failure);
|
||||
return;
|
||||
end;
|
||||
|
||||
loop
|
||||
declare
|
||||
Line : String := Get_Line (Input);
|
||||
begin
|
||||
-- You can process the contents of Line here.
|
||||
Put_Line (Output, Line);
|
||||
end;
|
||||
end loop;
|
||||
Close (Input);
|
||||
Close (Output);
|
||||
exception
|
||||
when End_Error =>
|
||||
if Is_Open(Input) then
|
||||
Close (Input);
|
||||
end if;
|
||||
if Is_Open(Output) then
|
||||
Close (Output);
|
||||
end if;
|
||||
end Read_And_Write_File_Line_By_Line;
|
||||
26
Task/File-input-output/Ada/file-input-output-3.ada
Normal file
26
Task/File-input-output/Ada/file-input-output-3.ada
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
with Ada.Sequential_IO;
|
||||
|
||||
procedure Read_And_Write_File_Character_By_Character is
|
||||
package Char_IO is new Ada.Sequential_IO (Character);
|
||||
use Char_IO;
|
||||
|
||||
Input, Output : File_Type;
|
||||
Buffer : Character;
|
||||
begin
|
||||
Open (File => Input, Mode => In_File, Name => "input.txt");
|
||||
Create (File => Output, Mode => Out_File, Name => "output.txt");
|
||||
loop
|
||||
Read (File => Input, Item => Buffer);
|
||||
Write (File => Output, Item => Buffer);
|
||||
end loop;
|
||||
Close (Input);
|
||||
Close (Output);
|
||||
exception
|
||||
when End_Error =>
|
||||
if Is_Open(Input) then
|
||||
Close (Input);
|
||||
end if;
|
||||
if Is_Open(Output) then
|
||||
Close (Output);
|
||||
end if;
|
||||
end Read_And_Write_File_Character_By_Character;
|
||||
24
Task/File-input-output/Ada/file-input-output-4.ada
Normal file
24
Task/File-input-output/Ada/file-input-output-4.ada
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Text_IO.Text_Streams; use Ada.Text_IO.Text_Streams;
|
||||
|
||||
procedure Using_Text_Streams is
|
||||
Input, Output : File_Type;
|
||||
Buffer : Character;
|
||||
begin
|
||||
Open (File => Input, Mode => In_File, Name => "input.txt");
|
||||
Create (File => Output, Mode => Out_File, Name => "output.txt");
|
||||
loop
|
||||
Buffer := Character'Input (Stream (Input));
|
||||
Character'Write (Stream (Output), Buffer);
|
||||
end loop;
|
||||
Close (Input);
|
||||
Close (Output);
|
||||
exception
|
||||
when End_Error =>
|
||||
if Is_Open(Input) then
|
||||
Close (Input);
|
||||
end if;
|
||||
if Is_Open(Output) then
|
||||
Close (Output);
|
||||
end if;
|
||||
end Using_Text_Streams;
|
||||
11
Task/File-input-output/Aime/file-input-output.aime
Normal file
11
Task/File-input-output/Aime/file-input-output.aime
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
file i, o;
|
||||
text s;
|
||||
|
||||
f_open(i, "input.txt", OPEN_READONLY, 0);
|
||||
f_open(o, "output.txt", OPEN_CREATE | OPEN_TRUNCATE | OPEN_WRITEONLY,
|
||||
0644);
|
||||
|
||||
while (f_line(i, s) ^ -1) {
|
||||
f_text(o, s);
|
||||
f_byte(o, '\n');
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
on copyFile from src into dst
|
||||
set filedata to read file src
|
||||
set outfile to open for access dst with write permission
|
||||
write filedata to outfile
|
||||
close access outfile
|
||||
end copyFile
|
||||
|
||||
copyFile from ":input.txt" into ":output.txt"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Loop, Read, input.txt, output.txt
|
||||
FileAppend, %A_LoopReadLine%`n
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
FileRead, var, input.txt
|
||||
FileAppend, %var%, output.txt
|
||||
|
|
@ -0,0 +1 @@
|
|||
FileCopy, input.txt, output.txt
|
||||
9
Task/File-input-output/BASIC/file-input-output-1.basic
Normal file
9
Task/File-input-output/BASIC/file-input-output-1.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
OPEN "INPUT.TXT" FOR INPUT AS #1
|
||||
OPEN "OUTPUT.TXT" FOR OUTPUT AS #2
|
||||
DO UNTIL EOF(1)
|
||||
LINE INPUT #1, Data$
|
||||
PRINT #2, Data$
|
||||
LOOP
|
||||
CLOSE #1
|
||||
CLOSE #2
|
||||
SYSTEM
|
||||
20
Task/File-input-output/BASIC/file-input-output-2.basic
Normal file
20
Task/File-input-output/BASIC/file-input-output-2.basic
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
100 I$ = "INPUT.TXT"
|
||||
110 O$ = "OUTPUT.TXT"
|
||||
120 D$ = CHR$(4)
|
||||
130 M$ = CHR$(13)
|
||||
140 PRINT D$"VERIFY"I$
|
||||
150 PRINT D$"OPEN"O$M$D$"CLOSE"O$M$D$"DELETE"O$
|
||||
160 PRINT D$"OPEN"O$M$D$"OPEN"I$;
|
||||
|
||||
170 PRINT M$D$"READ"I$
|
||||
180 ONERR GOTO 250
|
||||
190 GET C$
|
||||
200 POKE 216,0
|
||||
210 PRINT M$D$"WRITE"O$",B"B
|
||||
220 IF C$ <> M$ THEN PRINT C$;
|
||||
230 B = B + 1
|
||||
240 GOTO 170
|
||||
|
||||
250 POKE 216,0
|
||||
260 IF PEEK(222) <> 5 THEN RESUME
|
||||
270 PRINT M$D$"CLOSE"I$M$D$"CLOSE"O$
|
||||
1
Task/File-input-output/BBC-BASIC/file-input-output-1.bbc
Normal file
1
Task/File-input-output/BBC-BASIC/file-input-output-1.bbc
Normal file
|
|
@ -0,0 +1 @@
|
|||
*COPY input.txt output.txt
|
||||
7
Task/File-input-output/BBC-BASIC/file-input-output-2.bbc
Normal file
7
Task/File-input-output/BBC-BASIC/file-input-output-2.bbc
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
infile% = OPENIN("input.txt")
|
||||
outfile% = OPENOUT("output.txt")
|
||||
WHILE NOT EOF#infile%
|
||||
BPUT #outfile%, BGET#infile%
|
||||
ENDWHILE
|
||||
CLOSE #infile%
|
||||
CLOSE #outfile%
|
||||
4
Task/File-input-output/Babel/file-input-output.pb
Normal file
4
Task/File-input-output/Babel/file-input-output.pb
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(main
|
||||
{ "input.txt" >>> -- File is now on stack
|
||||
foo set -- File is now in 'foo'
|
||||
foo "output.txt" <<< })
|
||||
|
|
@ -0,0 +1 @@
|
|||
copy input.txt output.txt
|
||||
|
|
@ -0,0 +1 @@
|
|||
type input.txt > output.txt
|
||||
|
|
@ -0,0 +1 @@
|
|||
for /f "" %L in ('more^<input.txt') do echo %L>>output.txt
|
||||
1
Task/File-input-output/Befunge/file-input-output.bf
Normal file
1
Task/File-input-output/Befunge/file-input-output.bf
Normal file
|
|
@ -0,0 +1 @@
|
|||
0110"txt.tupni"#@i10"txt.tuptuo"#@o@
|
||||
1
Task/File-input-output/Bracmat/file-input-output.bracmat
Normal file
1
Task/File-input-output/Bracmat/file-input-output.bracmat
Normal file
|
|
@ -0,0 +1 @@
|
|||
put$(get$"input.txt","output.txt",NEW)
|
||||
28
Task/File-input-output/C++/file-input-output-1.cpp
Normal file
28
Task/File-input-output/C++/file-input-output-1.cpp
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
string line;
|
||||
ifstream input ( "input.txt" );
|
||||
ofstream output ("output.txt");
|
||||
|
||||
if (output.is_open()) {
|
||||
if (input.is_open()){
|
||||
while (getline (input,line)) {
|
||||
output << line << endl;
|
||||
}
|
||||
input.close(); // Not necessary - will be closed when variable goes out of scope.
|
||||
}
|
||||
else {
|
||||
cout << "input.txt cannot be opened!\n";
|
||||
}
|
||||
output.close(); // Not necessary - will be closed when variable goes out of scope.
|
||||
}
|
||||
else {
|
||||
cout << "output.txt cannot be written to!\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
29
Task/File-input-output/C++/file-input-output-2.cpp
Normal file
29
Task/File-input-output/C++/file-input-output-2.cpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream input("input.txt");
|
||||
if (!input.is_open())
|
||||
{
|
||||
std::cerr << "could not open input.txt for reading.\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
std::ofstream output("output.txt");
|
||||
if (!output.is_open())
|
||||
{
|
||||
std::cerr << "could not open output.txt for writing.\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
output << input.rdbuf();
|
||||
if (!output)
|
||||
{
|
||||
std::cerr << "error copying the data.\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
10
Task/File-input-output/C++/file-input-output-3.cpp
Normal file
10
Task/File-input-output/C++/file-input-output-3.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# include <algorithm>
|
||||
# include <fstream>
|
||||
|
||||
int main() {
|
||||
std::ifstream ifile("input.txt");
|
||||
std::ofstream ofile("output.txt");
|
||||
std::copy(std::istreambuf_iterator<char>(ifile),
|
||||
std::istreambuf_iterator<char>(),
|
||||
std::ostreambuf_iterator<char>(ofile));
|
||||
}
|
||||
8
Task/File-input-output/C++/file-input-output-4.cpp
Normal file
8
Task/File-input-output/C++/file-input-output-4.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <fstream>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream input("input.txt");
|
||||
std::ofstream output("output.txt");
|
||||
output << input.rdbuf();
|
||||
}
|
||||
27
Task/File-input-output/C/file-input-output-1.c
Normal file
27
Task/File-input-output/C/file-input-output-1.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
FILE *in, *out;
|
||||
int c;
|
||||
|
||||
in = fopen("input.txt", "r");
|
||||
if (!in) {
|
||||
fprintf(stderr, "Error opening input.txt for reading.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
out = fopen("output.txt", "w");
|
||||
if (!out) {
|
||||
fprintf(stderr, "Error opening output.txt for writing.\n");
|
||||
fclose(in);
|
||||
return 1;
|
||||
}
|
||||
|
||||
while ((c = fgetc(in)) != EOF) {
|
||||
fputc(c, out);
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
fclose(in);
|
||||
return 0;
|
||||
}
|
||||
36
Task/File-input-output/C/file-input-output-2.c
Normal file
36
Task/File-input-output/C/file-input-output-2.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* we just return a yes/no status; caller can check errno */
|
||||
int copy_file(const char *in, const char *out)
|
||||
{
|
||||
int ret = 0;
|
||||
int fin, fout;
|
||||
ssize_t len;
|
||||
char *buf[4096]; /* buffer size, some multiple of block size preferred */
|
||||
struct stat st;
|
||||
|
||||
if ((fin = open(in, O_RDONLY)) == -1) return 0;
|
||||
if (fstat(fin, &st)) goto bail;
|
||||
|
||||
/* open output with same permission */
|
||||
fout = open(out, O_WRONLY|O_CREAT|O_TRUNC, st.st_mode & 0777);
|
||||
if (fout == -1) goto bail;
|
||||
|
||||
while ((len = read(fin, buf, 4096)) > 0)
|
||||
write(fout, buf, len);
|
||||
|
||||
ret = len ? 0 : 1; /* last read should be 0 */
|
||||
|
||||
bail: if (fin != -1) close(fin);
|
||||
if (fout != -1) close(fout);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
copy_file("infile", "outfile");
|
||||
return 0;
|
||||
}
|
||||
23
Task/File-input-output/C/file-input-output-3.c
Normal file
23
Task/File-input-output/C/file-input-output-3.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
int copy_file(const char *in, const char *out)
|
||||
{
|
||||
int ret = 0;
|
||||
int fin, fout;
|
||||
char *bi;
|
||||
struct stat st;
|
||||
|
||||
if ((fin = open(in, O_RDONLY)) == -1) return 0;
|
||||
if (fstat(fin, &st)) goto bail;
|
||||
|
||||
fout = open(out, O_WRONLY|O_CREAT|O_TRUNC, st.st_mode & 0777);
|
||||
if (fout == -1) goto bail;
|
||||
|
||||
bi = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fin, 0);
|
||||
|
||||
ret = (bi == (void*)-1)
|
||||
? 0 : (write(fout, bi, st.st_size) == st.st_size);
|
||||
|
||||
bail: if (fin != -1) close(fin);
|
||||
if (fout != -1) close(fout);
|
||||
if (bi != (void*)-1) munmap(bi, st.st_size);
|
||||
return ret;
|
||||
}
|
||||
48
Task/File-input-output/COBOL/file-input-output-1.cobol
Normal file
48
Task/File-input-output/COBOL/file-input-output-1.cobol
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. file-io.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
INPUT-OUTPUT SECTION.
|
||||
FILE-CONTROL.
|
||||
SELECT in-file ASSIGN "input.txt"
|
||||
ORGANIZATION LINE SEQUENTIAL.
|
||||
|
||||
SELECT OPTIONAL out-file ASSIGN "output.txt"
|
||||
ORGANIZATION LINE SEQUENTIAL.
|
||||
|
||||
DATA DIVISION.
|
||||
FILE SECTION.
|
||||
FD in-file.
|
||||
01 in-line PIC X(256).
|
||||
|
||||
FD out-file.
|
||||
01 out-line PIC X(256).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DECLARATIVES.
|
||||
in-file-error SECTION.
|
||||
USE AFTER ERROR ON in-file.
|
||||
DISPLAY "An error occurred while using input.txt."
|
||||
GOBACK
|
||||
.
|
||||
out-file-error SECTION.
|
||||
USE AFTER ERROR ON out-file.
|
||||
DISPLAY "An error occurred while using output.txt."
|
||||
GOBACK
|
||||
.
|
||||
END DECLARATIVES.
|
||||
|
||||
mainline.
|
||||
OPEN INPUT in-file
|
||||
OPEN OUTPUT out-file
|
||||
|
||||
PERFORM FOREVER
|
||||
READ in-file
|
||||
AT END
|
||||
EXIT PERFORM
|
||||
END-READ
|
||||
WRITE out-line FROM in-line
|
||||
END-PERFORM
|
||||
|
||||
CLOSE in-file, out-file
|
||||
.
|
||||
2
Task/File-input-output/COBOL/file-input-output-2.cobol
Normal file
2
Task/File-input-output/COBOL/file-input-output-2.cobol
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*> Originally from ACUCOBOL-GT
|
||||
CALL "C$COPY" USING "input.txt", "output.txt", 0
|
||||
2
Task/File-input-output/COBOL/file-input-output-3.cobol
Normal file
2
Task/File-input-output/COBOL/file-input-output-3.cobol
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*> Originally from Micro Focus COBOL
|
||||
CALL "CBL_COPY_FILE" USING "input.txt", "output.txt"
|
||||
19
Task/File-input-output/Clean/file-input-output-1.clean
Normal file
19
Task/File-input-output/Clean/file-input-output-1.clean
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import StdEnv
|
||||
|
||||
copyFile fromPath toPath world
|
||||
# (ok, fromFile, world) = fopen fromPath FReadData world
|
||||
| not ok = abort ("Cannot open " +++ fromPath +++ " for reading")
|
||||
# (ok, toFile, world) = fopen toPath FWriteData world
|
||||
| not ok = abort ("Cannot open " +++ toPath +++ " for writing")
|
||||
# (fromFile, toFile) = copyData 1024 fromFile toFile
|
||||
# (ok, world) = fclose fromFile world
|
||||
| not ok = abort ("Cannot close " +++ fromPath +++ " after reading")
|
||||
# (ok, world) = fclose toFile world
|
||||
| not ok = abort ("Cannot close " +++ toPath +++ " after writing")
|
||||
= world
|
||||
where
|
||||
copyData bufferSize fromFile toFile
|
||||
# (buffer, fromFile) = freads fromFile bufferSize
|
||||
# toFile = fwrites buffer toFile
|
||||
| size buffer < bufferSize = (fromFile, toFile) // we're done
|
||||
= copyData bufferSize fromFile toFile // continue recursively
|
||||
1
Task/File-input-output/Clean/file-input-output-2.clean
Normal file
1
Task/File-input-output/Clean/file-input-output-2.clean
Normal file
|
|
@ -0,0 +1 @@
|
|||
Start world = copyFile "input.txt" "output.txt" world
|
||||
3
Task/File-input-output/Clojure/file-input-output-1.clj
Normal file
3
Task/File-input-output/Clojure/file-input-output-1.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(use 'clojure.java.io)
|
||||
|
||||
(copy (file "input.txt") (file "output.txt"))
|
||||
5
Task/File-input-output/Clojure/file-input-output-2.clj
Normal file
5
Task/File-input-output/Clojure/file-input-output-2.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
;; simple file writing
|
||||
(spit "filename.txt" "your content here")
|
||||
|
||||
;; simple file reading
|
||||
(slurp "filename.txt")
|
||||
4
Task/File-input-output/ColdFusion/file-input-output.cfm
Normal file
4
Task/File-input-output/ColdFusion/file-input-output.cfm
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<cfif fileExists(expandPath("input.txt"))>
|
||||
<cffile action="read" file="#expandPath('input.txt')#" variable="inputContents">
|
||||
<cffile action="write" file="#expandPath('output.txt')#" output="#inputContents#">
|
||||
</cfif>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(with-open-file (in #p"input.txt" :direction :input)
|
||||
(with-open-file (out #p"output.txt" :direction :output)
|
||||
(loop for line = (read-line in nil 'foo)
|
||||
until (eq line 'foo)
|
||||
do (write-line line out))))
|
||||
12
Task/File-input-output/Common-Lisp/file-input-output-2.lisp
Normal file
12
Task/File-input-output/Common-Lisp/file-input-output-2.lisp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(defconstant +buffer-size+ (expt 2 16))
|
||||
|
||||
(with-open-file (in #p"input.txt" :direction :input
|
||||
:element-type '(unsigned-byte 8))
|
||||
(with-open-file (out #p"output.txt"
|
||||
:direction :output
|
||||
:element-type (stream-element-type in))
|
||||
(loop with buffer = (make-array +buffer-size+
|
||||
:element-type (stream-element-type in))
|
||||
for size = (read-sequence buffer in)
|
||||
while (plusp size)
|
||||
do (write-sequence buffer out :end size))))
|
||||
5
Task/File-input-output/D/file-input-output-1.d
Normal file
5
Task/File-input-output/D/file-input-output-1.d
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import std.file: copy;
|
||||
|
||||
void main() {
|
||||
copy("input.txt", "output.txt");
|
||||
}
|
||||
15
Task/File-input-output/D/file-input-output-2.d
Normal file
15
Task/File-input-output/D/file-input-output-2.d
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import std.stdio;
|
||||
|
||||
int main() {
|
||||
auto from = File("input.txt", "rb");
|
||||
scope(exit) from.close();
|
||||
|
||||
auto to = File("output.txt", "wb");
|
||||
scope(exit) to.close();
|
||||
|
||||
foreach(buffer; from.byChunk(new ubyte[4096*1024])) {
|
||||
to.rawWrite(buffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Task/File-input-output/D/file-input-output-3.d
Normal file
9
Task/File-input-output/D/file-input-output-3.d
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import tango.io.device.File;
|
||||
|
||||
void main()
|
||||
{
|
||||
auto from = new File("input.txt");
|
||||
auto to = new File("output.txt", File.WriteCreate);
|
||||
to.copy(from).close;
|
||||
from.close;
|
||||
}
|
||||
7
Task/File-input-output/D/file-input-output-4.d
Normal file
7
Task/File-input-output/D/file-input-output-4.d
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import tango.io.device.File;
|
||||
|
||||
void main()
|
||||
{
|
||||
auto to = new File("output.txt", File.WriteCreate);
|
||||
to.copy(new File("input.txt")).close;
|
||||
}
|
||||
11
Task/File-input-output/Delphi/file-input-output-1.delphi
Normal file
11
Task/File-input-output/Delphi/file-input-output-1.delphi
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var
|
||||
f : TextFile ;
|
||||
s : string ;
|
||||
begin
|
||||
AssignFile(f,[fully qualified file name);
|
||||
Reset(f);
|
||||
writeln(f,s);
|
||||
Reset(f);
|
||||
ReadLn(F,S);
|
||||
CloseFile(
|
||||
end;
|
||||
10
Task/File-input-output/Delphi/file-input-output-2.delphi
Normal file
10
Task/File-input-output/Delphi/file-input-output-2.delphi
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
var
|
||||
f : File ;
|
||||
buff : array[1.1024] of byte ;
|
||||
BytesRead : Integer ;
|
||||
begin
|
||||
AssignFile(f,fully qualified file name);
|
||||
Reset(f,1);
|
||||
Blockread(f,Buff,SizeOf(Buff),BytesRead);
|
||||
CloseFile(f);
|
||||
end;
|
||||
27
Task/File-input-output/Delphi/file-input-output-3.delphi
Normal file
27
Task/File-input-output/Delphi/file-input-output-3.delphi
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
type
|
||||
|
||||
tAddressBook = Record
|
||||
FName : string[20];
|
||||
LName : string[30];
|
||||
Address : string[30];
|
||||
City : string[30];
|
||||
State : string[2];
|
||||
Zip5 : string[5];
|
||||
Zip4 : string[4];
|
||||
Phone : string[14];
|
||||
Deleted : boolean ;
|
||||
end;
|
||||
|
||||
var
|
||||
f : file of tAddressBook ;
|
||||
v : tAddressBook ;
|
||||
bytes : integer ;
|
||||
begin
|
||||
AssignFile(f,fully qualified file name);
|
||||
Reset(f);
|
||||
Blockread(f,V,1,Bytes);
|
||||
Edit(v);
|
||||
Seek(F,FilePos(f)-1);
|
||||
BlockWrite(f,v,1,bytes);
|
||||
CloseFile(f);
|
||||
end;
|
||||
1
Task/File-input-output/E/file-input-output.e
Normal file
1
Task/File-input-output/E/file-input-output.e
Normal file
|
|
@ -0,0 +1 @@
|
|||
<file:output.txt>.setText(<file:input.txt>.getText())
|
||||
33
Task/File-input-output/Eiffel/file-input-output.e
Normal file
33
Task/File-input-output/Eiffel/file-input-output.e
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
do
|
||||
create input_file.make_open_read ("input.txt")
|
||||
create output_file.make_open_write ("output.txt")
|
||||
|
||||
from
|
||||
input_file.read_character
|
||||
until
|
||||
input_file.exhausted
|
||||
loop
|
||||
output_file.put (input_file.last_character)
|
||||
input_file.read_character
|
||||
end
|
||||
|
||||
input_file.close
|
||||
output_file.close
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
input_file: PLAIN_TEXT_FILE
|
||||
output_file: PLAIN_TEXT_FILE
|
||||
|
||||
end
|
||||
7
Task/File-input-output/Erlang/file-input-output.erl
Normal file
7
Task/File-input-output/Erlang/file-input-output.erl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
-module( file_io ).
|
||||
|
||||
-export( [task/0] ).
|
||||
|
||||
task() ->
|
||||
{ok, Contents} = file:read_file( "input.txt" ),
|
||||
ok = file:write_file( "output.txt", Contents ).
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
include std/io.e
|
||||
write_lines("output.txt", read_lines("input.txt"))
|
||||
16
Task/File-input-output/Euphoria/file-input-output-2.euphoria
Normal file
16
Task/File-input-output/Euphoria/file-input-output-2.euphoria
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
integer in,out
|
||||
object line
|
||||
|
||||
in = open("input.txt","r")
|
||||
out = open("output.txt","w")
|
||||
|
||||
while 1 do
|
||||
line = gets(in)
|
||||
if atom(line) then -- EOF reached
|
||||
exit
|
||||
end if
|
||||
puts(out,line)
|
||||
end while
|
||||
|
||||
close(out)
|
||||
close(in)
|
||||
2
Task/File-input-output/Factor/file-input-output-1.factor
Normal file
2
Task/File-input-output/Factor/file-input-output-1.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
"input.txt" binary file-contents
|
||||
"output.txt" binary set-file-contents
|
||||
4
Task/File-input-output/Factor/file-input-output-2.factor
Normal file
4
Task/File-input-output/Factor/file-input-output-2.factor
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[
|
||||
"input.txt" binary <file-reader> &dispose
|
||||
"output.txt" binary <file-writer> stream-copy
|
||||
] with-destructors
|
||||
1
Task/File-input-output/Factor/file-input-output-3.factor
Normal file
1
Task/File-input-output/Factor/file-input-output-3.factor
Normal file
|
|
@ -0,0 +1 @@
|
|||
"input.txt" "output.txt" copy-file
|
||||
14
Task/File-input-output/Forth/file-input-output.fth
Normal file
14
Task/File-input-output/Forth/file-input-output.fth
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
\ <to> <from> copy-file
|
||||
: copy-file ( a1 n1 a2 n2 -- )
|
||||
r/o open-file throw >r
|
||||
w/o create-file throw r>
|
||||
begin
|
||||
pad maxstring 2 pick read-file throw
|
||||
?dup while
|
||||
pad swap 3 pick write-file throw
|
||||
repeat
|
||||
close-file throw
|
||||
close-file throw ;
|
||||
|
||||
\ Invoke it like this:
|
||||
s" output.txt" s" input.txt" copy-file
|
||||
21
Task/File-input-output/Fortran/file-input-output.f
Normal file
21
Task/File-input-output/Fortran/file-input-output.f
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
program FileIO
|
||||
|
||||
integer, parameter :: out = 123, in = 124
|
||||
integer :: err
|
||||
character(len=1) :: c
|
||||
|
||||
open(out, file="output.txt", status="new", action="write", access="stream", iostat=err)
|
||||
if ( err == 0 ) then
|
||||
open(in, file="input.txt", status="old", action="read", access="stream", iostat=err)
|
||||
if ( err == 0 ) then
|
||||
err = 0
|
||||
do while ( err == 0 )
|
||||
read(unit=in, iostat=err) c
|
||||
if ( err == 0 ) write(out) c
|
||||
end do
|
||||
close(in)
|
||||
end if
|
||||
close(out)
|
||||
end if
|
||||
|
||||
end program FileIO
|
||||
4
Task/File-input-output/Frink/file-input-output.frink
Normal file
4
Task/File-input-output/Frink/file-input-output.frink
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
contents = read["file:input.txt"]
|
||||
w = new Writer["output.txt"]
|
||||
w.print[contents]
|
||||
w.close[]
|
||||
15
Task/File-input-output/GAP/file-input-output.gap
Normal file
15
Task/File-input-output/GAP/file-input-output.gap
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
CopyFile := function(src, dst)
|
||||
local f, g, line;
|
||||
f := InputTextFile(src);
|
||||
g := OutputTextFile(dst, false);
|
||||
while true do
|
||||
line := ReadLine(f);
|
||||
if line = fail then
|
||||
break
|
||||
else
|
||||
WriteLine(g, Chomp(line));
|
||||
fi;
|
||||
od;
|
||||
CloseStream(f);
|
||||
CloseStream(g);
|
||||
end;
|
||||
18
Task/File-input-output/GML/file-input-output.gml
Normal file
18
Task/File-input-output/GML/file-input-output.gml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
var file, str;
|
||||
file = file_text_open_read("input.txt");
|
||||
str = "";
|
||||
while (!file_text_eof(file))
|
||||
{
|
||||
str += file_text_read_string(file);
|
||||
if (!file_text_eof(file))
|
||||
{
|
||||
str += "
|
||||
"; //It is important to note that a linebreak is actually inserted here rather than a character code of some kind
|
||||
file_text_readln(file);
|
||||
}
|
||||
}
|
||||
file_text_close(file);
|
||||
|
||||
file = file_text_open_write("output.txt");
|
||||
file_text_write_string(file,str);
|
||||
file_text_close(file);
|
||||
2
Task/File-input-output/GUISS/file-input-output.guiss
Normal file
2
Task/File-input-output/GUISS/file-input-output.guiss
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Start,My Documents,Rightclick:input.txt,Copy,Menu,Edit,Paste,
|
||||
Rightclick:Copy of input.txt,Rename,Type:output.txt[enter]
|
||||
17
Task/File-input-output/Go/file-input-output-1.go
Normal file
17
Task/File-input-output/Go/file-input-output-1.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b, err := ioutil.ReadFile("input.txt")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err = ioutil.WriteFile("output.txt", b, 0666); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
37
Task/File-input-output/Go/file-input-output-2.go
Normal file
37
Task/File-input-output/Go/file-input-output-2.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func CopyFile(out, in string) (err error) {
|
||||
var inf, outf *os.File
|
||||
inf, err = os.Open(in)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
cErr := inf.Close()
|
||||
if err == nil {
|
||||
err = cErr
|
||||
}
|
||||
}()
|
||||
outf, err = os.Create(out)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = io.Copy(outf, inf)
|
||||
cErr := outf.Close()
|
||||
if err == nil {
|
||||
err = cErr
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := CopyFile("output.txt", "input.txt"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
1
Task/File-input-output/Groovy/file-input-output-1.groovy
Normal file
1
Task/File-input-output/Groovy/file-input-output-1.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
new File('output.txt').write(new File('input.txt').text)
|
||||
1
Task/File-input-output/Groovy/file-input-output-2.groovy
Normal file
1
Task/File-input-output/Groovy/file-input-output-2.groovy
Normal file
|
|
@ -0,0 +1 @@
|
|||
new AntBuilder().copy(file:'input.txt', toFile:'output.txt', overwrite:true)
|
||||
3
Task/File-input-output/Groovy/file-input-output-3.groovy
Normal file
3
Task/File-input-output/Groovy/file-input-output-3.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
new File('output.txt').withWriter( w ->
|
||||
new File('input.txt').withReader( r -> w << r }
|
||||
}
|
||||
1
Task/File-input-output/Haskell/file-input-output.hs
Normal file
1
Task/File-input-output/Haskell/file-input-output.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
main = readFile "input.txt" >>= writeFile "output.txt"
|
||||
2
Task/File-input-output/HicEst/file-input-output-1.hicest
Normal file
2
Task/File-input-output/HicEst/file-input-output-1.hicest
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
CHARACTER input='input.txt ', output='output.txt ', c, buffer*4096
|
||||
SYSTEM(COPY=input//output, ERror=11) ! on error branch to label 11 (not shown)
|
||||
7
Task/File-input-output/HicEst/file-input-output-2.hicest
Normal file
7
Task/File-input-output/HicEst/file-input-output-2.hicest
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
OPEN(FIle=input, OLD, ERror=21) ! on error branch to label 21 (not shown)
|
||||
OPEN(FIle=output)
|
||||
DO i = 1, 1E300 ! "infinite" loop, exited on end-of-file error
|
||||
READ( FIle=input, ERror=22) buffer ! on error (end of file) branch to label 22
|
||||
WRITE(FIle=output, ERror=23) buffer ! on error branch to label 23 (not shown)
|
||||
ENDDO
|
||||
22 WRITE(FIle=output, CLoSe=1)
|
||||
5
Task/File-input-output/HicEst/file-input-output-3.hicest
Normal file
5
Task/File-input-output/HicEst/file-input-output-3.hicest
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
OPEN(FIle=input, SEQuential, UNFormatted, OLD, LENgth=len, ERror=31) ! on error branch to label 31 (not shown)
|
||||
OPEN(FIle=output, SEQuential, UNFormatted, ERror=32) ! on error branch to label 32 (not shown)
|
||||
ALLOCATE(c, len)
|
||||
READ(FIle=input, CLoSe=1) c
|
||||
WRITE(FIle=output, CLoSe=1) c END
|
||||
12
Task/File-input-output/IDL/file-input-output.idl
Normal file
12
Task/File-input-output/IDL/file-input-output.idl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
; open two LUNs
|
||||
openw,unit1,'output.txt,/get
|
||||
openr,unit2,'input.txt',/get
|
||||
; how many bytes to read
|
||||
fs = fstat(unit2)
|
||||
; make buffer
|
||||
buff = bytarr(fs.size)
|
||||
; transfer content
|
||||
readu,unit2,buff
|
||||
writeu,unit1,buff
|
||||
; that's all
|
||||
close,/all
|
||||
5
Task/File-input-output/Icon/file-input-output.icon
Normal file
5
Task/File-input-output/Icon/file-input-output.icon
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
procedure main()
|
||||
in := open(f := "input.txt","r") | stop("Unable to open ",f)
|
||||
out := open(f := "output.txt","w") | stop("Unable to open ",f)
|
||||
while write(out,read(in))
|
||||
end
|
||||
9
Task/File-input-output/Io/file-input-output.io
Normal file
9
Task/File-input-output/Io/file-input-output.io
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
inf := File with("input.txt") openForReading
|
||||
outf := File with("output.txt") openForUpdating
|
||||
|
||||
while(l := inf readLine,
|
||||
outf write(l, "\n")
|
||||
)
|
||||
|
||||
inf close
|
||||
outf close
|
||||
1
Task/File-input-output/J/file-input-output-1.j
Normal file
1
Task/File-input-output/J/file-input-output-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
'output.txt' (1!:2~ 1!:1)&< 'input.txt'
|
||||
2
Task/File-input-output/J/file-input-output-2.j
Normal file
2
Task/File-input-output/J/file-input-output-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
require 'files'
|
||||
'output.txt' (fwrite~ fread) 'input.txt'
|
||||
18
Task/File-input-output/Java/file-input-output-1.java
Normal file
18
Task/File-input-output/Java/file-input-output-1.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import java.io.*;
|
||||
|
||||
public class FileIODemo {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
FileInputStream in = new FileInputStream("input.txt");
|
||||
FileOutputStream out = new FileOutputStream("ouput.txt");
|
||||
int c;
|
||||
while ((c = in.read()) != -1) {
|
||||
out.write(c);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Task/File-input-output/Java/file-input-output-2.java
Normal file
30
Task/File-input-output/Java/file-input-output-2.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import java.io.*;
|
||||
|
||||
public class FileIODemo2 {
|
||||
public static void main(String args[]) {
|
||||
try {
|
||||
// Probably should wrap with a BufferedInputStream
|
||||
final InputStream in = new FileInputStream("input.txt");
|
||||
try {
|
||||
// Probably should wrap with a BufferedOutputStream
|
||||
final OutputStream out = new FileOutputStream("output.txt");
|
||||
try {
|
||||
int c;
|
||||
while ((c = in.read()) != -1) {
|
||||
out.write(c);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Task/File-input-output/Java/file-input-output-3.java
Normal file
26
Task/File-input-output/Java/file-input-output-3.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import java.io.*;
|
||||
import java.nio.channels.*;
|
||||
|
||||
public class FileIODemo3 {
|
||||
public static void main(String args[]) {
|
||||
try {
|
||||
final FileChannel in = new FileInputStream("input.txt").getChannel();
|
||||
try {
|
||||
final FileChannel out = new FileOutputStream("output.txt").getChannel();
|
||||
try {
|
||||
out.transferFrom(in, 0, in.size());
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.err.println("Exception while trying to copy: "+e);
|
||||
e.printStackTrace(); // stack trace of place where it happened
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Task/File-input-output/Java/file-input-output-4.java
Normal file
14
Task/File-input-output/Java/file-input-output-4.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import java.io.*;
|
||||
public class Test {
|
||||
public static void main (String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
bw.write(line);
|
||||
bw.newLine();
|
||||
}
|
||||
br.close();
|
||||
bw.close();
|
||||
}
|
||||
}
|
||||
9
Task/File-input-output/Java/file-input-output-5.java
Normal file
9
Task/File-input-output/Java/file-input-output-5.java
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import java.nio.file.*;
|
||||
public class Copy{
|
||||
public static void main(String[] args) throws Exception{
|
||||
FileSystem fs = FileSystems.getDefault();
|
||||
Path in = fs.getPath("input.txt");
|
||||
Path out = fs.getPath("output.txt");
|
||||
Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
15
Task/File-input-output/JavaScript/file-input-output-1.js
Normal file
15
Task/File-input-output/JavaScript/file-input-output-1.js
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
var fso = new ActiveXObject("Scripting.FileSystemObject");
|
||||
var ForReading = 1, ForWriting = 2;
|
||||
var f_in = fso.OpenTextFile('input.txt', ForReading);
|
||||
var f_out = fso.OpenTextFile('output.txt', ForWriting, true);
|
||||
|
||||
// for small files:
|
||||
// f_out.Write( f_in.ReadAll() );
|
||||
|
||||
while ( ! f_in.AtEndOfStream) {
|
||||
// ReadLine() does not include the newline char
|
||||
f_out.WriteLine( f_in.ReadLine() );
|
||||
}
|
||||
|
||||
f_in.Close();
|
||||
f_out.Close();
|
||||
2
Task/File-input-output/JavaScript/file-input-output-2.js
Normal file
2
Task/File-input-output/JavaScript/file-input-output-2.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var fs = require('fs');
|
||||
require('util').pump(fs.createReadStream('input.txt', {flags:'r'}), fs.createWriteStream('output.txt', {flags:'w+'}));
|
||||
2
Task/File-input-output/Julia/file-input-output-1.julia
Normal file
2
Task/File-input-output/Julia/file-input-output-1.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
string = open(readall, "file1", "r")
|
||||
open(io->write(io, string), "file2", "w")
|
||||
1
Task/File-input-output/Julia/file-input-output-2.julia
Normal file
1
Task/File-input-output/Julia/file-input-output-2.julia
Normal file
|
|
@ -0,0 +1 @@
|
|||
cp("file1","file2")
|
||||
5
Task/File-input-output/Julia/file-input-output-3.julia
Normal file
5
Task/File-input-output/Julia/file-input-output-3.julia
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
infile = open("file1", "r")
|
||||
outfile = open("file2", "w")
|
||||
write(outfile, readall(infile))
|
||||
close(outfile)
|
||||
close(infile)
|
||||
1
Task/File-input-output/Julia/file-input-output-4.julia
Normal file
1
Task/File-input-output/Julia/file-input-output-4.julia
Normal file
|
|
@ -0,0 +1 @@
|
|||
open(IO ->write(IO, open(readall, "file1", "r")), "file2", "w")
|
||||
1
Task/File-input-output/K/file-input-output.k
Normal file
1
Task/File-input-output/K/file-input-output.k
Normal file
|
|
@ -0,0 +1 @@
|
|||
`output.txt 0:0:`input.txt
|
||||
7
Task/File-input-output/Lang5/file-input-output.lang5
Normal file
7
Task/File-input-output/Lang5/file-input-output.lang5
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
: puts(*) . "\n" . ;
|
||||
: set-file '> swap open ;
|
||||
: >>contents slurp puts ;
|
||||
: copy-file
|
||||
swap set-file 'fdst set fdst fout >>contents fdst close ;
|
||||
|
||||
'output.txt 'input.txt copy-file
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
nomainwin
|
||||
|
||||
open "input.txt" for input as #f1
|
||||
qtyBytes = lof( #f1)
|
||||
source$ = input$( #f1, qtyBytes)
|
||||
close #f1
|
||||
|
||||
open "output.txt" for output as #f2
|
||||
#f2 source$;
|
||||
close #f2
|
||||
|
||||
end
|
||||
32
Task/File-input-output/Lisaac/file-input-output.lisaac
Normal file
32
Task/File-input-output/Lisaac/file-input-output.lisaac
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
Section Header
|
||||
|
||||
+ name := FILE_IO;
|
||||
|
||||
Section Public
|
||||
|
||||
- main <- (
|
||||
+ e : ENTRY;
|
||||
+ f : STD_FILE;
|
||||
+ s : STRING;
|
||||
|
||||
e := FILE_SYSTEM.get "input.txt";
|
||||
(e != NULL).if {
|
||||
f ?= e.open_read_only;
|
||||
(f != NULL).if {
|
||||
s := STRING.create(f.size);
|
||||
f.read s size (f.size);
|
||||
f.close;
|
||||
};
|
||||
};
|
||||
|
||||
(s != NULL).if {
|
||||
e := FILE_SYSTEM.make_file "output.txt";
|
||||
(e != NULL).if {
|
||||
f ?= e.open;
|
||||
(f != NULL).if {
|
||||
f.write s from (s.lower) size (s.count);
|
||||
f.close;
|
||||
};
|
||||
};
|
||||
};
|
||||
);
|
||||
10
Task/File-input-output/Logo/file-input-output.logo
Normal file
10
Task/File-input-output/Logo/file-input-output.logo
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
to copy :from :to
|
||||
openread :from
|
||||
openwrite :to
|
||||
setread :from
|
||||
setwrite :to
|
||||
until [eof?] [print readrawline]
|
||||
closeall
|
||||
end
|
||||
|
||||
copy "input.txt "output.txt
|
||||
12
Task/File-input-output/Lua/file-input-output.lua
Normal file
12
Task/File-input-output/Lua/file-input-output.lua
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
inFile = io.open("input.txt", "r")
|
||||
data = inFile:read("*all") -- may be abbreviated to "*a";
|
||||
-- other options are "*line",
|
||||
-- or the number of characters to read.
|
||||
inFile:close()
|
||||
|
||||
outFile = io.open("output.txt", "w")
|
||||
outfile:write(data)
|
||||
outfile:close()
|
||||
|
||||
-- Oneliner version:
|
||||
io.open("output.txt", "w"):write(io.open("input.txt", "r"):read("*a"))
|
||||
8
Task/File-input-output/MAXScript/file-input-output.max
Normal file
8
Task/File-input-output/MAXScript/file-input-output.max
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
inFile = openFile "input.txt"
|
||||
outFile = createFile "output.txt"
|
||||
while not EOF inFile do
|
||||
(
|
||||
format "%" (readLine inFile) to:outFile
|
||||
)
|
||||
close inFile
|
||||
close outFile
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
SetDirectory@NotebookDirectory[];
|
||||
If[FileExistsQ["output.txt"], DeleteFile["output.txt"], Print["No output yet"] ];
|
||||
CopyFile["input.txt", "output.txt"]
|
||||
40
Task/File-input-output/Mercury/file-input-output.mercury
Normal file
40
Task/File-input-output/Mercury/file-input-output.mercury
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
:- module file_io.
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
|
||||
main(!IO) :-
|
||||
io.open_input("input.txt", InputRes, !IO),
|
||||
(
|
||||
InputRes = ok(Input),
|
||||
io.read_file_as_string(Input, ReadRes, !IO),
|
||||
(
|
||||
ReadRes = ok(Contents),
|
||||
io.close_input(Input, !IO),
|
||||
io.open_output("output.txt", OutputRes, !IO),
|
||||
(
|
||||
OutputRes = ok(Output),
|
||||
io.write_string(Output, Contents, !IO),
|
||||
io.close_output(Output, !IO)
|
||||
;
|
||||
OutputRes = error(OutputError),
|
||||
print_io_error(OutputError, !IO)
|
||||
)
|
||||
;
|
||||
ReadRes = error(_, ReadError),
|
||||
print_io_error(ReadError, !IO)
|
||||
)
|
||||
;
|
||||
InputRes = error(InputError),
|
||||
print_io_error(InputError, !IO)
|
||||
).
|
||||
|
||||
:- pred print_io_error(io.error::in, io::di, io::uo) is det.
|
||||
|
||||
print_io_error(Error, !IO) :-
|
||||
io.stderr_stream(Stderr, !IO),
|
||||
io.write_string(Stderr, io.error_message(Error), !IO),
|
||||
io.set_exit_status(1, !IO).
|
||||
19
Task/File-input-output/Modula-3/file-input-output.mod3
Normal file
19
Task/File-input-output/Modula-3/file-input-output.mod3
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
MODULE FileIO EXPORTS Main;
|
||||
|
||||
IMPORT IO, Rd, Wr;
|
||||
|
||||
<*FATAL ANY*>
|
||||
|
||||
VAR
|
||||
infile: Rd.T;
|
||||
outfile: Wr.T;
|
||||
txt: TEXT;
|
||||
|
||||
BEGIN
|
||||
infile := IO.OpenRead("input.txt");
|
||||
outfile := IO.OpenWrite("output.txt");
|
||||
txt := Rd.GetText(infile, LAST(CARDINAL));
|
||||
Wr.PutText(outfile, txt);
|
||||
Rd.Close(infile);
|
||||
Wr.Close(outfile);
|
||||
END FileIO.
|
||||
27
Task/File-input-output/NetRexx/file-input-output.netrexx
Normal file
27
Task/File-input-output/NetRexx/file-input-output.netrexx
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
import java.nio.
|
||||
|
||||
parse arg infileName outfileName .
|
||||
|
||||
if infileName = '' | infileName.length = 0 then infileName = 'data/input.txt'
|
||||
if outfileName = '' | outfileName.length = 0 then outfileName = 'data/output.txt'
|
||||
|
||||
binaryCopy(infileName, outfileName)
|
||||
|
||||
return
|
||||
|
||||
method binaryCopy(infileName, outfileName) public static
|
||||
|
||||
do
|
||||
infile = Paths.get('.', [String infileName])
|
||||
outfile = Paths.get('.', [String outfileName])
|
||||
fileOctets = Files.readAllBytes(infile)
|
||||
Files.write(outfile, fileOctets, [StandardOpenOption.WRITE, StandardOpenOption.CREATE])
|
||||
|
||||
catch ioex = IOException
|
||||
ioex.printStackTrace()
|
||||
end
|
||||
|
||||
return
|
||||
13
Task/File-input-output/OCaml/file-input-output-1.ocaml
Normal file
13
Task/File-input-output/OCaml/file-input-output-1.ocaml
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
let () =
|
||||
let ic = open_in "input.txt" in
|
||||
let oc = open_out "output.txt" in
|
||||
try
|
||||
while true do
|
||||
let s = input_line ic in
|
||||
output_string oc s;
|
||||
output_char oc '\n';
|
||||
done
|
||||
with End_of_file ->
|
||||
close_in ic;
|
||||
close_out oc;
|
||||
;;
|
||||
12
Task/File-input-output/OCaml/file-input-output-2.ocaml
Normal file
12
Task/File-input-output/OCaml/file-input-output-2.ocaml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
let () =
|
||||
let ic = open_in "input.txt" in
|
||||
let oc = open_out "output.txt" in
|
||||
try
|
||||
while true do
|
||||
let c = input_char ic in
|
||||
output_char oc c
|
||||
done
|
||||
with End_of_file ->
|
||||
close_in ic;
|
||||
close_out oc;
|
||||
;;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue