Fixed length records: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 6 users not shown)
Line 50:
=={{header|Ada}}==
Ada string type is a fixed-length string.
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
 
Line 92:
end loop;
end Main;
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 121:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIXED_LENGTH_RECORDS.AWK
BEGIN {
Line 147:
return( substr(str,start,1) revstr(str,start-1) )
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 173:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <cstdlib>
#include <fstream>
Line 208:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 226:
=={{header|COBOL}}==
 
<langsyntaxhighlight lang="cobol"> *> Rosetta Code, fixed length records
*> Tectonics:
*> cobc -xj lrecl80.cob
Line 314:
 
goback.
end program lrecl80.</langsyntaxhighlight>
 
Given a starting file sample.txt of
Line 351:
Demonstrate text to Forth source block form.
 
<langsyntaxhighlight lang="cobol"> *> Rosetta Code fixed length records, text to Forth block
identification division.
program-id. blocking.
Line 440:
 
goback.
end program blocking.</langsyntaxhighlight>
 
Demonstrate Forth source block to text form.
 
<langsyntaxhighlight lang="cobol"> *> Rosetta Code fixed length records, Forth blocks to text.
identification division.
program-id. unblocking.
Line 515:
 
goback.
end program unblocking.</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 525:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Const As Byte longRegistro = 80
Const archivoEntrada As String = "infile.dat"
Line 561:
Close
Sleep
</syntaxhighlight>
</lang>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 618:
check(err)
fmt.Println(string(bytes))
}</langsyntaxhighlight>
 
{{out}}
Line 635:
'''Bonus round'''
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 709:
block2text("block.dat", "block.txt")
text2block("block.txt", "block2.dat")
}</langsyntaxhighlight>
 
=={{header|J}}==
Line 715:
 
Using the 720 byte input file linked to in the Raku entry.
<langsyntaxhighlight lang="j"> _80 ]\ fread 'flr-infile.dat' NB. reads the file into a n by 80 array
_80 |.\ fread 'flr-infile.dat' NB. as above but reverses each 80 byte chunk
'flr-outfile.dat' fwrite~ , _80 |.\ fread 'flr-infile.dat' NB. as above but writes result to file (720 bytes)
processFixLenFile=: fwrite~ [: , _80 |.\ fread NB. represent operation as a verb/function</langsyntaxhighlight>
 
'''Example Usage:'''
 
<langsyntaxhighlight lang="j"> 'flr-outfile.dat' processFixLenFile 'flr-infile.dat' NB. returns number of bytes written
720</langsyntaxhighlight>
 
=={{header|jq}}==
 
To read the raw input and write out raw strings, the command-line options -R (raw input), -sr ("slurp"raw output), and -r (raw output)j options are necessary:
 
jq -RrsRrj -f program.jq infile.dat
 
program.jq:
 
Currently gojq does not include the `_nwise/1` filter provided by jq, and in any case jq's implementation is suboptimal,
For the sake of generality, we define `cut` with an argument corresponding to the number of columns. To illustrate that an efficient recursive definition is possible, we define an inner helper function, `c`, with arity 0, as currently jq's tail-call optimization is restricted to zero-arity filters:
so we define `nwise` here so that it can be used for both array and string inputs. Notice that the inner helper function, `n`, has arity 0, allowing jq's tail-call optimization to work.
<syntaxhighlight lang="jq">def cut($n):
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
nwise(80) | explode | reverse | implode
</syntaxhighlight>
To show that this does indeed work, the output of `/bin/dd cbs=80 conv=unblock` is shown:
{{output}}
<pre>
8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
 
6 eniL
<lang jq>def cut($n):
7 eniL
def c: if length==0 then empty else .[:$n] , (.[$n:] | c) end;
............................................................8 enil detnednI
c;
NIGRAM TR 9 eniL
 
1+1 records in
cut(80) | explode | reverse | implode;</lang>
1+1 records out
644 bytes transferred in 0.030319 secs (21241 bytes/sec)
</pre>
 
=={{header|Julia}}==
The program reads from an "infile.dat" file created with dd, as described in the task instructions.
<langsyntaxhighlight lang="julia">
function processfixedlengthrecords(infname, blocksize, outfname)
inf = open(infname)
Line 757 ⟶ 774:
processfixedlengthrecords("infile.dat", 80, "outfile.dat")
</syntaxhighlight>
</lang>
 
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- prep: convert given sample text to fixed length "infile.dat"
local sample = [[
Line 1...1.........2.........3.........4.........5.........6.........7.........8
Line 789 ⟶ 806:
 
-- output:
os.execute("dd if=outfile.dat cbs=80 conv=unblock")</langsyntaxhighlight>
{{out}}
<pre>8.........7.........6.........5.........4.........3.........2.........1...1 eniL
Line 807 ⟶ 824:
Form Buffer object (hold a memory block) we can read a byte at offset. So Print Eval(Line80,3) return the 4th byte, and Return Line80, 2:=255,5:=0 set two bytes (unsigned, we place any number and interpreter convert it to byte).
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module FixedFile {
Read fixed$
Line 898 ⟶ 915:
FixedFile "fixed.random"
 
</syntaxhighlight>
</lang>
===Forth Blocks===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Form 80,50
Print "Forth's Blocks"
Line 1,022 ⟶ 1,039:
Page1=NewBlock()
Return
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<syntaxhighlight lang="mathematica">
<lang Mathematica>
FixedRecordReverse[inFile_File, outFile_File, length_ : 80] :=
Module[{inStream, outStream, line, byte},
Line 1,052 ⟶ 1,069:
RunProcess[{"dd", "if=" <> outFile[[1]], "cbs=" <> ToString[length], "conv=unblock"}, "StandardOutput"]
];
</syntaxhighlight>
</lang>
 
This function will both return the reversed string in the notebook AND write the record to outfile.dat:
Line 1,076 ⟶ 1,093:
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
fixed length records, in Neko
*/
Line 1,114 ⟶ 1,131:
}
file_close(input)
file_close(output)</langsyntaxhighlight>
 
{{out}}
Line 1,136 ⟶ 1,153:
===Task: fixed length records===
 
<langsyntaxhighlight Nimlang="nim">import algorithm
 
proc reverse(infile, outfile: string) =
Line 1,155 ⟶ 1,172:
raise newException(IOError, "truncated data when writing")
 
reverse("infile.dat", "outfile.dat")</langsyntaxhighlight>
 
{{out}}
Line 1,170 ⟶ 1,187:
 
===Bonus: Forth blocks===
<langsyntaxhighlight Nimlang="nim">import strutils
 
const EmptyRecord = repeat(' ', 64)
Line 1,224 ⟶ 1,241:
when isMainModule:
textToBlock("block1.txt", "block.dat")
blockToText("block.dat", "block2.txt")</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program reverseFixedLines(input, output, stdErr);
const
lineLength = 80;
Line 1,248 ⟶ 1,265:
writeLn();
end;
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">open $in, '<', 'flr-infile.dat';
open $out, '>', 'flr-outfile.dat';
 
Line 1,258 ⟶ 1,275:
print reverse($record)."\n" # display reversed records, line-by-line
}
close $out;</langsyntaxhighlight>
{{out}}
<pre>8.........7.........6.........5.........4.........3.........2.........1...1 eniL
Line 1,274 ⟶ 1,291:
===There Is More Than One Way To Do It===
Read with standard <> using $/ set to a ref-to-int to read a fixed block size.
<langsyntaxhighlight lang="perl">{
local ($/, @ARGV) = (\80, 'infile.dat');
open my $out, '>', 'outfile.dat' or die $!;
print $out scalar reverse while <>; # can read fixed length too :)
close $out;
}</langsyntaxhighlight>
Slurp and reverse each line in place.
<langsyntaxhighlight lang="perl">use Path::Tiny;
path('outfile.dat')->spew(path('infile.dat')->slurp =~ s/.{80}/reverse $&/gesr);</langsyntaxhighlight>
Double reverse.
<langsyntaxhighlight lang="perl">use Path::Tiny;
path('outfile.dat')->spew(reverse unpack '(a80)*', reverse path('infile.dat')->slurp);
</syntaxhighlight>
</lang>
Bonus round: convert the sample file to Forth Block format.
<langsyntaxhighlight lang="perl">use Path::Tiny;
path('outfile.dat')->spew(pack '(A64)16', split /\n/, path('sample.txt')->slurp);</langsyntaxhighlight>
Bonus Round: convert Forth Block format to plain text format.
<langsyntaxhighlight lang="perl">use Path::Tiny;
path('sample2.txt')->spew(map "$_\n", unpack '(A64)16', path('outfile.dat')->slurp);</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 1,298 ⟶ 1,315:
never really designed for fixed length record handling and for me, for this task, their limits
outweight any advantages. To simplify matters, this creates any files needed on the fly.
<!--<syntaxhighlight lang="phix">(notonline)-->
<lang Phix>constant sample_text = """
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- file i/o</span>
Line 1...1.........2.........3.........4.........5.........6.........7.........8
<span style="color: #008080;">constant</span> <span style="color: #000000;">sample_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Line 2
Line 1...1.........2.........3.........4.........5.........6.........7.........8
Line 3
Line 42
Line 3
 
Line 64
Line 7
Line 6
Indented line 8............................................................
Line 7
Line 9 RT MARGIN"""
Indented line 8............................................................
 
Line 9 RT MARGIN"""</span>
constant indat = "infile.dat",
outdat = "outfile.dat"
<span style="color: #008080;">constant</span> <span style="color: #000000;">indat</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"infile.dat"</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">outdat</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"outfile.dat"</span>
if not file_exists(indat) then
object text = get_text("sample.txt")
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">file_exists</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if text=-1 then text = sample_text end if
<span style="color: #004080;">object</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"sample.txt"</span><span style="color: #0000FF;">)</span>
sequence s = split(text,'\n')
<span style="color: #008080;">if</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sample_text</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer fn = open(indat,"wb")
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"wb"</span><span style="color: #0000FF;">)</span>
puts(fn,s[i]&repeat(' ',80-length(s[i])))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">80</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])))</span>
close(fn)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"%s created (%d bytes)\n",{indat,get_file_size(indat)})
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s created (%d bytes)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">get_file_size</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">)})</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
function get_block(integer fn, size)
string res = ""
<span style="color: #008080;">function</span> <span style="color: #000000;">get_block</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">size</span><span style="color: #0000FF;">)</span>
for i=1 to size do
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
res &= getc(fn)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">size</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">getc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
integer fn = open(indat,"rb"),
isize = get_file_size(indat)
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rb"</span><span style="color: #0000FF;">),</span>
puts(1,"reversed:\n")
<span style="color: #000000;">isize</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_file_size</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">)</span>
for i=1 to isize by 80 do
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"reversed:\n"</span><span style="color: #0000FF;">)</span>
?reverse(get_block(fn,80))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">isize</span> <span style="color: #008080;">by</span> <span style="color: #000000;">80</span> <span style="color: #008080;">do</span>
end for
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_block</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">80</span><span style="color: #0000FF;">))</span>
close(fn)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
-- Bonus part, 16*64 (=1024) block handling:
 
<span style="color: #000080;font-style:italic;">-- Bonus part, 16*64 (=1024) block handling:</span>
procedure put_block(integer fn, sequence lines)
lines &= repeat("",16-length(lines))
<span style="color: #008080;">procedure</span> <span style="color: #000000;">put_block</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
for i=1 to length(lines) do
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">))</span>
string li = lines[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if length(li)>64 then
<span style="color: #004080;">string</span> <span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
li = li[1..64]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)></span><span style="color: #000000;">64</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">li</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">64</span><span style="color: #0000FF;">]</span>
li &= repeat(' ',64-length(li))
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">li</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">64</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">))</span>
puts(fn,li)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
fn = open(outdat,"wb")
put_block(fn,split(sample_text,'\n'))
<span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">outdat</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"wb"</span><span style="color: #0000FF;">)</span>
close(fn)
<span style="color: #000000;">put_block</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sample_text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">))</span>
integer osize = get_file_size(outdat)
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
printf(1,"\n%s created (%d bytes):\n",{outdat,osize})
<span style="color: #004080;">integer</span> <span style="color: #000000;">osize</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_file_size</span><span style="color: #0000FF;">(</span><span style="color: #000000;">outdat</span><span style="color: #0000FF;">)</span>
fn = open(outdat,"rb")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n%s created (%d bytes):\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">outdat</span><span style="color: #0000FF;">,</span><span style="color: #000000;">osize</span><span style="color: #0000FF;">})</span>
sequence lines = {}
<span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">outdat</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"rb"</span><span style="color: #0000FF;">)</span>
for i=1 to osize by 64 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
string line = get_block(fn,64)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">osize</span> <span style="color: #008080;">by</span> <span style="color: #000000;">64</span> <span style="color: #008080;">do</span>
?line
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_block</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">64</span><span style="color: #0000FF;">)</span>
lines = append(lines,trim_tail(line,' '))
<span style="color: #0000FF;">?</span><span style="color: #000000;">line</span>
end for
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">,</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">))</span>
lines = trim_tail(lines,{""})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,"\ntrimmed:\n")
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">})</span>
pp(lines,{pp_Nest,1})
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\ntrimmed:\n"</span><span style="color: #0000FF;">)</span>
{} = delete_file(indat) -- (for consistent 2nd run)</lang>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">delete_file</span><span style="color: #0000FF;">(</span><span style="color: #000000;">indat</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (for consistent 2nd run)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,416 ⟶ 1,436:
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
infile = open('infile.dat', 'rb')
outfile = open('outfile.dat', 'wb')
Line 1,429 ⟶ 1,449:
infile.close()
outfile.close()
</syntaxhighlight>
</lang>
 
Output:
Line 1,453 ⟶ 1,473:
Essentially the same as task [[Selective_File_Copy]] except more boring.
 
<syntaxhighlight lang="raku" perl6line>$*OUT = './flr-outfile.dat'.IO.open(:w, :bin) orelse .die; # open a file in binary mode for writing
while my $record = $*IN.read(80) { # read in fixed sized binary chunks
$*OUT.write: $record.=reverse; # write reversed records out to $outfile
$*ERR.say: $record.decode('ASCII'); # display decoded records on STDERR
}
close $*OUT;</langsyntaxhighlight>
{{out}}
<pre>8.........7.........6.........5.........4.........3.........2.........1...1 eniL
Line 1,471 ⟶ 1,491:
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
<lang REBOL>
inp: open %infile.dat
out: open/new %outfile.dat
Line 1,479 ⟶ 1,499:
close inp
close out
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm reads fixed─length 80 byte records; reverses each record, displays to term. */
iFID= 'FIXEDLEN.TXT' /*the file's filename (used for input).*/
call charin iFID, 1, 0 /*open the file, point rec pointer to 1*/
Line 1,492 ⟶ 1,512:
do k=1 for # /* [↓] process all the records read. */
say reverse(@.k) /* reverse a record and write to term. */
end /*k*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input file:}}
{{out}}
Line 1,507 ⟶ 1,527:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">open("outfile.dat", "w") do |out_f|
open("infile.dat") do |in_f|
while record = in_f.read(80)
out_f << record.reverse
end
end
end # both files automatically closed
</syntaxhighlight>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader, BufWriter};
Line 1,533 ⟶ 1,562:
Err(error) => eprintln!("I/O error: {}", error),
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,550 ⟶ 1,579:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">chan configure stdin -translation binary
chan configure stdout -translation binary
 
Line 1,560 ⟶ 1,589:
# while {[set line [read stdin 80]] ne ""} {
# puts -nonewline [string reverse $line]
# }</langsyntaxhighlight>
 
{{out}}
Line 1,574 ⟶ 1,603:
............................................................8 enil detnednI
NIGRAM TR 9 eniL</pre>
 
=={{header|TXR}}==
 
===80 Column Task===
 
At the shell prompt:
 
<pre>
$ txr -e '(let ((buf (make-buf 80))
(nread 80))
(while (eql nread 80)
(set nread (fill-buf-adjust buf))
(buf-set-length buf 80)
(put-buf (nreverse buf))))' < infile80.bin > outfile80.bin
$ dd if=outfile80.bin cbs=80 conv=unblock
8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
 
6 eniL
7 eniL
............................................................8 enil detnednI
NIGRAM TR 9 eniL
 
1+1 records in
1+1 records out
725 bytes copied, 8.658e-05 s, 8.4 MB/s
</pre>
 
This handles a final bit that is shorter than 80. When <code>buf-set-length</code> sets the buffer size back to 80, zero padding is applied. The zero-padded record is reversed.
 
===Forth Blocks Task===
 
====Encoding====
 
The following program is called <code>forth-enblock.tl</code>:
 
<syntaxhighlight lang="txrlisp">(typedef forth-line (array 64 bchar))
 
(let ((lno 0)
(blanks (make-buf 64 #\space)))
(whilet ((line (get-line)))
(put-obj (fmt "~-64,64a" line) (ffi forth-line))
(inc lno))
(while (plusp (mod (pinc lno) 16))
(put-buf blanks)))</syntaxhighlight>
 
{{out}}
 
<pre>$ txr forth-enblock.tl < forth-enblock.tl > forth-enblock.blk
$ xxd forth-enblock.blk
00000000: 2874 7970 6564 6566 2066 6f72 7468 2d6c (typedef forth-l
00000010: 696e 6520 2861 7272 6179 2036 3420 6263 ine (array 64 bc
00000020: 6861 7229 2920 2020 2020 2020 2020 2020 har))
00000030: 2020 2020 2020 2020 2020 2020 2020 2020
00000040: 2020 2020 2020 2020 2020 2020 2020 2020
00000050: 2020 2020 2020 2020 2020 2020 2020 2020
00000060: 2020 2020 2020 2020 2020 2020 2020 2020
00000070: 2020 2020 2020 2020 2020 2020 2020 2020
00000080: 286c 6574 2028 286c 6e6f 2030 2920 2020 (let ((lno 0)
[... snip ...]
000003e0: 2020 2020 2020 2020 2020 2020 2020 2020
000003f0: 2020 2020 2020 2020 2020 2020 2020 2020</pre>
 
====Decoding====
 
This is <code>forth-deblock.tl</code>
 
<syntaxhighlight lang="txrlisp">(typedef forth-block (array 16 (array 64 bchar)))
 
(defsymacro fbsz (sizeof forth-block))
 
(let* ((buf (make-buf fbsz))
(block-view (carray-buf buf (ffi forth-block)))
(nread fbsz))
(while (eql fbsz nread)
(set nread (fill-buf-adjust buf))
(when (plusp nread)
(buf-set-length buf fbsz #\space)
(each ((row [block-view 0]))
(put-line (trim-right #/ +/ row))))))</syntaxhighlight>
 
{{out}}
 
<pre>$ txr forth-deblock.tl < forth-enblock.blk
(typedef forth-line (array 64 bchar))
 
(let ((lno 0)
(blanks (make-buf 64 #\space)))
(whilet ((line (get-line)))
(put-obj (fmt "~-64,64a" line) (ffi forth-line))
(inc lno))
(while (plusp (mod (pinc lno) 16))
(put-buf blanks)))
 
 
 
 
 
 
 
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./str" for Str
 
var records = File.read("infile.dat")
Line 1,588 ⟶ 1,720:
}
records = File.read("outfile.dat")
for (record in Str.chunks(records, 80)) System.print(record)</langsyntaxhighlight>
 
{{out}}
Line 1,605 ⟶ 1,737:
===Bonus round===
{{libheader|Wren-ioutil}}
<langsyntaxhighlight ecmascriptlang="wren">import "./ioutil" for File, FileUtil
import "./str" for Str
 
var blockToText = Fn.new { |blockFileName, textFileName|
Line 1,636 ⟶ 1,768:
blockToText.call("data.blk", "data.txt")
textToBlock.call("data.txt", "data2.blk")
System.print(FileUtil.areDuplicates("data.blk", "data2.blk"))</langsyntaxhighlight>
 
{{out}}
<pre>
true
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib;
int N, I;
char A(80);
[OpenInFile("infile.dat");
OpenOutFile("outfile.dat");
for N:= 1 to 9 do
[for I:= 0 to 80-1 do A(I):= ChIn(3);
for I:= 80-1 downto 0 do ChOut(3, A(I));
];
OpenInFile("outfile.dat");
for N:= 1 to 9 do
[for I:= 0 to 80-1 do ChOut(0, ChIn(3)); CrLf(0)];
]</syntaxhighlight>
{{out}}
<pre>
8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
6 eniL
7 eniL
............................................................8 enil detnednI
NIGRAM TR 9 eniL
</pre>
 
Line 1,648 ⟶ 1,807:
Line 1...1.........2.........3.........4.........5.........6.........7.........8Line 2 Line 3 Line 4 Line 6 Line 7 Indented line 8............................................................Line 9 RT MARGIN
</pre>
<langsyntaxhighlight lang="zkl">File("infile.dat","rb") // could contain nulls and newlines
// Since we are writing to a ASCII terminal, ignore nulls
.walker(3).chunk(80,String).pump(Console.println,"reverse"); // 3-->read chars</langsyntaxhighlight>
{{out}}
<pre>
Line 1,664 ⟶ 1,823:
</pre>
To write to a file (as one big line), preserving nulls:
<langsyntaxhighlight lang="zkl">in,out := File("infile.dat","rb"), File("outfile.dat","wb");
in.walker(0).chunk(80).pump(out,"reverse"); // may contain nulls and newlines
// 0-->read bytes, chunk to list of bytes, reverse and write the bytes
in.close(); out.close();</langsyntaxhighlight>
outfile.dat:
{{out}}
Line 1,674 ⟶ 1,833:
</pre>
<b>Bonus Round</b>
<langsyntaxhighlight lang="zkl">// read block file (as in read a file of blocks) to text
fcn readFourthBlock(inFileNm){
out,f,buf := Sink(String), File(inFileNm,"rb"), Data(1024);
Line 1,682 ⟶ 1,841:
}
f.close(); out.close();
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">// read text file and write as block to file
fcn formatToFourthBlock(inFileNm,outFileNm){
n,blk,in,out := 0, Data(), File(inFileNm,"r"), File(outFileNm,"wb");
Line 1,693 ⟶ 1,852:
if(blk) out.write(blk, " "*(1024 - blk.len()));
f.close(); out.close();
}</langsyntaxhighlight>
9,488

edits