How to resolve the algorithm Fixed length records step by step in the Wren programming language
How to resolve the algorithm Fixed length records step by step in the Wren programming language
Table of Contents
Problem Statement
Fixed length read/write Before terminals, computers commonly used punch card readers or paper tape input. A common format before these devices were superseded by terminal technology was based on the Hollerith code, Hollerith code. These input devices handled 80 columns per card and had a limited character set, encoded by punching holes in one or more rows of the card for each column. These devices assumed/demanded a fixed line width of 80 characters, newlines were not required (and could not even be encoded in some systems). Write a program to read 80 column fixed length records (no newline terminators (but newline characters allowed in the data)) and then write out the reverse of each line as fixed length 80 column records. Samples here use printable characters, but that is not a given with fixed length data. Filenames used are sample.txt, infile.dat, outfile.dat. Note: There are no newlines, inputs and outputs are fixed at 80 columns, no more, no less, space padded. Fixed length data is 8 bit complete. NUL bytes of zero are allowed. These fixed length formats are still in wide use on mainframes, with JCL and with COBOL (which commonly use EBCDIC encoding and not ASCII). Most of the large players in day to day financial transactions know all about fixed length records and the expression logical record length. To create the sample input file, use an editor that supports fixed length records or use a conversion utility. For instance, most GNU/Linux versions of dd support blocking and unblocking records with a conversion byte size.
Forth systems often include BLOCK words. A block is 1024 bytes. Source code is stored as 16 lines of 64 characters each (again, no newline character or sequence to mark the end of a line). Write a program to convert a block file to text (using newlines). Trailing spaces should be excluded from the output. Also demonstrate how to convert from a normal text file to block form. All lines either truncated or padded to 64 characters with no newline terminators. The last block filled to be exactly 1024 characters by adding blanks if needed. Assume a full range of 8 bit byte values for each character. The COBOL example uses forth.txt and forth.blk filenames.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fixed length records step by step in the Wren programming language
Source code in the wren programming language
import "io" for File
import "./str" for Str
var records = File.read("infile.dat")
File.create("outfile.dat") { |f|
for (record in Str.chunks(records, 80)) {
record = record[-1..0]
f.writeBytes(record)
}
}
records = File.read("outfile.dat")
for (record in Str.chunks(records, 80)) System.print(record)
import "./ioutil" for File, FileUtil
import "./str" for Str
var blockToText = Fn.new { |blockFileName, textFileName|
var block = File.read(blockFileName)
var lb = FileUtil.lineBreak
File.create(textFileName) { |f|
for (chunk in Str.chunks(block, 64)) {
f.writeBytes(chunk.trimEnd() + lb)
}
}
}
var textToBlock = Fn.new { |textFileName, blockFileName|
var lines = FileUtil.readLines(textFileName).where { |l| l != "" }.toList
var text = lines.map { |l| (l.count < 64) ? l + (" " * (64-l.count)) : l[0..63] }.join()
var rem = text.count % 1024
if (rem > 0) text = text + (" " * (1024 - rem))
File.create(blockFileName) { |f| f.writeBytes(text) }
}
// create a block file
File.create("data.blk") { |f|
f.writeBytes("a" * 1024)
f.writeBytes("b" * 1024)
f.writeBytes("c" * 1024)
f.writeBytes("d" * 1024)
}
blockToText.call("data.blk", "data.txt")
textToBlock.call("data.txt", "data2.blk")
System.print(FileUtil.areDuplicates("data.blk", "data2.blk"))
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Quackery programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Nu programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Primes - allocate descendants to their ancestors step by step in the C++ programming language