Fixed length records: Difference between revisions

Adding example for Mathematica/WolframLanguage
(Adding example for Mathematica/WolframLanguage)
Line 1,023:
Return
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
 
<lang Mathematica>
RecordReverse[inFile_File, outFile_File] :=
Module[{inStream, outStream, line, byte},
WithCleanup[
inStream = OpenRead[inFile, BinaryFormat -> True];
outStream = OpenWrite[outFile, BinaryFormat -> True];
,
While[True,
line = {};
Do[
byte = BinaryRead[inStream, "Byte"];
AppendTo[line, byte]
,
80
];
If[byte === EndOfFile, Break[]];
line = Reverse[line];
BinaryWrite[outStream, line]
]
,
Close[outStream];
Close[inStream];
];
(* Verify the result *)
RunProcess[{"dd", "if=" <> outFile[[1]], "cbs=80", "conv=unblock"}, "StandardOutput"]
];
</lang>
 
This function will both return the reversed string in the notebook AND write the record to outfile.dat:
{{in}}
<pre>
In[]:= RecordReverse[File["infile.dat"], File["outfile.dat"]]
</pre>
{{out}}
<pre>
 
Out[]= "\
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>
 
=={{header|Neko}}==
Anonymous user