A common question asked in Delphi forums is how to read or write data from comma-delimeted or fixed length record text files.
There is a method for reading and writing records to comma-delimeted or fixed length record files that is seldom explored due to the lack of Delphi documentation on the subject. This is the use of a standard TTable component to read disk files as tables.
The basic concept is to designate the file as a table type of ttASCII, and perform operations on the records in the file just as with any conventional database table.
The description below shows how to use a TTable with (1) comma delimeted text records, and (2) fixed length data records.
These examples use a sample of inventory data, with field in the following order:
Date counted.
Item ID
Name of counter.
Quantity in stock (for sale)
Quantity returned by the store.
Quantity in lay-away.
Quantity in customer returns.
Comma-delimeted text files
Below is an example of records from a comma-delimeted text file. Each record is terminated with a carriage-return, line-feed.
This file will be named InvImport.txt. For ttASCII table types, the name of the file must end in the TXT extension.
Before this file can be used by a TTable component, you have to create a schema definition file. Schema files have the same name as the data file, with a SCH extension. When a TTable component is used with a ttASCII table type, the BDE looks for the SCH file that matches the TXT file.
Here is the composition of the file InvImport.sch:
Schema files are written in INI file format, and the first section tag is the name of the table, which always corresponds to the name of the data file. The FILETYPE label tag has two values, VARYING or FIXED. The FIXED table type is explained below. For comma-delimeted text, the DELIMETER tag identifies the single character used around character string values. The SEPARATOR tag identifies the single character used between fields. Each field is defined (in order from left to right) by Field# tags. The format of the labels must be consistent and sequential, and the value (definition) must consist of:
Field name.
Field type (one of CHAR, BOOL, DATE, FLOAT, LONGINT, NUMBER, TIME, TIMESTAMP)
Length (in VARYING files, this represents the maximum length of the field.
Decimal places (valid only for field type of FLOAT)
Offset (zero for VARYING files. See the fixed-length section below)
Next, we need to assign the attributes to the TTable component:
DatabaseName
(path to the data file)
TableName
InvImport.Txt
TableType
ttASCII
Now, simply hook this table up to a TDBGrid with a TDataSource, and when the TTable is activated, the data shown in the file extract above will be displayed in a 4 row by 7 column grid.
Fixed Length data files
Here is an example of the same data file, but as fixed-length records. Each record is terminated with a single character before the carriage-return, line-feed. This is to make sure that there are no varying lengths of white space before the CR/LF, which would change the exact length of each record.
2002/04/30 11:26:30 52319 Robert J. 405 17 0 0 x 2002/04/30 11:37:04 52416 Mark W. 4 0 0 2 x 2002/04/30 12:01:14 52512 Robert J. 94 0 0 8 x 2002/04/30 12:02:56 52582 Mark W. 3 0 0 0 x
The names of the data and schema files are the same as shown in Comma-delimeted text files above. The attributes of the TTable component will remain the same as well. The difference is in the composition of the schema file. Here is an example of the revised schema file InvImport.sch, set up to define the fixed-length text above.
In this schema definition, there is no need to include the DELIMETER or SEPARATOR tags, since they do not apply to fixed length data. Also, the field definitions have changed to reflect:
Exact length of the field
Offset to the beginning of the field
The offset is zero-based, and indicates the number of characters from the first field position to the beginning of the field. If the offset is not correct, it can cause a BDE error when the TTable is opened and the Fields are populated. For instance, in the definition above, if the offset for Field3 was 27, this would cause the first character of the WhoCounted field to be read as part of the ItemID field, causing a 'Data type conversion' error at run time.
An important note about the DATE, TIME and TIMESTAMP field types. The format for data in these fields must match the format specified in the BDE's Configuration settings under System. See the BDE Administrator tool for these format settings.
For additional information see: Delphi 5 Developer's Guide by Steve Teixeira and Xavier Pacheco