{"id":425,"date":"2011-06-23T19:31:36","date_gmt":"2011-06-23T19:31:36","guid":{"rendered":"http:\/\/craftydba.com\/?p=425"},"modified":"2024-02-17T14:25:47","modified_gmt":"2024-02-17T14:25:47","slug":"fixed-formatted-text-files","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=425","title":{"rendered":"Fixed Formatted Text Files"},"content":{"rendered":"<p>The invention of a fixed formatted text (flat) file layout started with <a href=\"http:\/\/en.wikipedia.org\/wiki\/Data_set_(IBM_mainframe)\">IBM<\/a> in the 1960&#8217;s.  Data sets on the mainframe could be stored in both fixed and variable length records.<\/p>\n<p>A <a href=\"http:\/\/en.wikipedia.org\/wiki\/Flat_file_database\">flat file<\/a> 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.<\/p>\n<p>While flat files can have either <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ascii\">ASCII<\/a> or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ebcdic\">EBCDIC<\/a> 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.<\/p>\n<p>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.<\/p>\n<p>The table below summarizes the methods and properties of the classes.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"500\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\">OpenDataFile()<\/td>\n<td style=\"border: thin solid gray;\">Read &#038; Write<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">CloseDataFile()<\/td>\n<td style=\"border: thin solid gray;\">Read &#038; Write<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">PushData()<\/td>\n<td style=\"border: thin solid gray;\">Write<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">PullData()<\/td>\n<td style=\"border: thin solid gray;\">Read<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">PullAllData()<\/td>\n<td style=\"border: thin solid gray;\">Read<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">RemoveFile()<\/td>\n<td style=\"border: thin solid gray;\">Write<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">FileExists()<\/td>\n<td style=\"border: thin solid gray;\">Read<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">EOF<\/td>\n<td style=\"border: thin solid gray;\">Read<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/P><\/p>\n<p>&nbsp;<\/P><\/p>\n<p>&nbsp;<\/P><\/p>\n<p>&nbsp;<\/P><\/p>\n<p>&nbsp;<\/P><\/p>\n<p>&nbsp;<\/P><\/p>\n<p>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.<\/p>\n<p>\nThe &#8220;PackField&#8221; 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.<\/p>\n<pre class=\"lang:VB theme:familiar mark:1,2-3\" title=\"vb script - packfield()\">\r\nPublic Function PackField(strData, intSize)\r\n   PackField = mid(\"\" & strData & Space(intSize), 1, intSize)\r\nEnd Function\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The &#8220;UnpackField&#8221; 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.<\/p>\n<pre class=\"lang:VB theme:familiar mark:1,2-3\" title=\"vb script - unpackfield()\">\r\nPublic Function UnpackField(strData, intLength)\r\n    If len(strData) < intLength Then\r\n        UnpackField = strData\r\n    Else\r\n        UnpackField = Mid(strData, 1, intLength)\r\n        strData = Mid(strData, intLength+1)\r\n    End If\r\nEnd Function\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>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.<\/p>\n<p>The table below has started code that you can use in your next Visual Basic (VB) Script.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"600\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/mod-write-text-file.vbs_.txt'>mod-write-text-file.vbs<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Write Text File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/mod-read-text-file.vbs_.txt'>mod-read-text-file.vbs<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Read Text File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/tst-write-flat-file.vbs_.txt'>tst-write-flat-file.vbs<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Write Flat File Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/tst-read-flat-file.vbs_.txt'>tst-read-flat-file.vbs<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Read Flat File Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/write-flat.cmd_.txt'>write-flat.cmd<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Write Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/read-flat.cmd_.txt'>read-flat.cmd<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Read Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/presidents.txt'>presidents<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Presidents Flat File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">\n<a href='https:\/\/craftydba.com\/wp-content\/uploads\/2011\/06\/flat-file-output.txt'>flat-file-output<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Read Program Output<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The invention of a fixed formatted text (flat) file layout started with IBM in the 1960&#8217;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&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[26,27,12,15,13],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-other","tag-fixed-format","tag-flat-files","tag-free-code","tag-john-f-miner-iii","tag-vb-script-2"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=425"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}