File input/output: Difference between revisions

Content deleted Content added
Added COBOL section. Corrected PL/I and R lang tags.
Line 569: Line 569:
(slurp "filename.txt")
(slurp "filename.txt")
</lang>
</lang>

=={{header|COBOL}}==
===Implementation===
{{works with|OpenCOBOL}}
<lang cobol> 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
.</lang>

===Built-in Subroutines===
{{works with|OpenCOBOL}}
{{works with|Visual COBOL}}
<lang cobol>*> Originally from ACUCOBOL-GT
CALL "C$COPY" USING "input.txt", "output.txt", 0</lang>
<lang cobol>*> Originally from Micro Focus COBOL
CALL "CBL_COPY_FILE" USING "input.txt", "output.txt"</lang>


=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
Line 1,553: Line 1,613:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>
<lang pli>
declare in file, out file;
declare in file, out file;


open file (in) title ('/INPUT.TXT,type(text),recsize(100)') input;
open file (in) title ('/INPUT.TXT,type(text),recsize(100)') input;
open file (out) title ('/OUTPUT.TXT,type(text),recsize(100) output;
open file (out) title ('/OUTPUT.TXT,type(text),recsize(100') output;
do forever;
do forever;
get file (in) edit (line) (L);
get file (in) edit (line) (L);
Line 1,690: Line 1,750:
If files are textual we can use <tt>readLines</tt> ("-1" means "read until the end")
If files are textual we can use <tt>readLines</tt> ("-1" means "read until the end")


<lang R>src <- file("input.txt", "r")
<lang rsplus>src <- file("input.txt", "r")
dest <- file("output.txt", "w")
dest <- file("output.txt", "w")


Line 1,699: Line 1,759:
If the files are not textual but "generic":
If the files are not textual but "generic":


<lang R>src <- file("input.txt", "rb")
<lang rsplus>src <- file("input.txt", "rb")
dest <- file("output.txt", "wb")
dest <- file("output.txt", "wb")


Line 1,709: Line 1,769:
Another simpler way is to use <tt>file.copy</tt>
Another simpler way is to use <tt>file.copy</tt>


<lang R>file.copy("input.txt", "output.txt", overwrite = FALSE)</lang>
<lang rsplus>file.copy("input.txt", "output.txt", overwrite = FALSE)</lang>


=={{header|Racket}}==
=={{header|Racket}}==