The invention of a fixed formatted text (flat) file layout started with IBM in the 1960’s. Data sets on the mainframe could be stored in both fixed and variable length records.
A flat file is defined as having one record of data per row that is ended with a new line. Each field inside the record is represented by a fixed number of characters.
While flat files can have either ASCII or EBCDIC encoding, I will be exploring the former. Another generalization of this format is to store numbers in their machine form, binary coding. I will not be talking about this option today.
I will be using the read and write text file classes created earlier to implement a solution for storing data on the first five Presidents of the United States.
The table below summarizes the methods and properties of the classes.
OpenDataFile() | Read & Write |
CloseDataFile() | Read & Write |
PushData() | Write |
PullData() | Read |
PullAllData() | Read |
RemoveFile() | Write |
FileExists() | Read |
EOF | Read |
The trick to this format is to use a string buffer and to place the fields at the correct positions. All numeric data has to be saved text which may or may not cause lose of percision. I wrote the following helper functions to solve this buffer manipulation.
The “PackField” helper function packages a variable length string into a fixed one. Any null issues are avoided by appending a empty string at the start of the expression.
1 2 3 |
Public Function PackField(strData, intSize) PackField = mid("" & strData & Space(intSize), 1, intSize) End Function |
The “UnpackField” helper function chops off the field at the start of the string and returns it. The orginal string is modified to remove from the record.
1 2 3 4 5 6 7 8 |
Public Function UnpackField(strData, intLength) If len(strData) < intLength Then UnpackField = strData Else UnpackField = Mid(strData, 1, intLength) strData = Mid(strData, intLength+1) End If End Function |
In summary, writing to and reading from fixed formatted text files is very easy. You might bump into this format when exchanging data with clients that use the IBM mainframe.
The table below has started code that you can use in your next Visual Basic (VB) Script.
mod-write-text-file.vbs | Write Text File Class |
mod-read-text-file.vbs | Read Text File Class |
tst-write-flat-file.vbs | Write Flat File Program |
tst-read-flat-file.vbs | Read Flat File Program |
write-flat.cmd | Write Batch File |
read-flat.cmd | Read Batch File |
presidents | Presidents Flat File |
flat-file-output | Read Program Output |
magnificent put up, very informative. I wonder why the other specialists of this sector don’t understand this. You should proceed your writing. I’m confident, you have a great readers’ base already!