Your full service technology partner! 
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using DataBDE   Print This     
  From the June 2012 Issue of Prestwood eMag
Delphi BDE:
Using BDE TTables to read or write text file records
Posted 19 years ago on 8/21/2002 and updated 10/30/2008
Take Away: How to read or write data from comma-delimeted or fixed length record text files in Borland Delphi.


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.

2002/04/30 11:26:30,52319,Robert J.,405,17,, 2002/04/30 11:37:04,52416,Mark W.,4,0,2, 2002/04/30 12:01:14,52512,Robert J.,94,0,0,8 2002/04/30 12:02:56,52582,Mark W.,3,,,

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:

[INVIMPORT] FILETYPE = VARYING CHARSET = ASCII DELIMITER = ' SEPARATOR = , Field1 = CountDate,CHAR,21,00,00 Field2 = ItemID,LONGINT,05,00,00 Field3 = WhoCounted,CHAR,35,00,00 Field4 = CntShelf,LONGINT,09,00,00 Field5 = CntReturns,LONGINT,09,00,00 Field6 = CntHeld,LONGINT,09,00,00 Field7 = CntBroken,LONGINT,09,00,00

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.
  • 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.

[INVIMPORT] FILETYPE = FIXED CHARSET = ASCII Field1 = CountDate,CHAR,20,00,00 Field2 = ItemID,LONGINT,06,00,20 Field3 = WhoCounted,CHAR,10,00,26 Field4 = CntShelf,LONGINT,04,00,36 Field5 = CntReturns,LONGINT,04,00,40 Field6 = CntHeld,LONGINT,04,00,44 Field7 = CntBroken,LONGINT,04,00,48 Field8 = LineTerm,CHAR,01,00,52

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

More Info

Definition:  BDE
Definition:  BDE Alias
KB Post:  BDE Best Practices: Safely Use Paradox Tables
KB Post:  BDE Broken? Recent Microsoft Betas may be the culprit
Download:  Borland Database Engine 5.202
Article:  Installing BDE using Merge Modules
KB Post:  Paradox Table Specifications
Article:  Repair Paradox Tables
Article:  Sub-Classing the BDE TQuery Component


Share a thought or comment...
Comment 1 of 3
That was a great article.
Posted 16 years ago

Comment 2 of 3

I've read the article today and I found it very usefull. Thanks.

Posted 10 years ago

Comment 3 of 3

Whenever i set Active:True of BDE TTable I always endup getting error "Data Structure Corruption" please help me out

Posted 7 years ago
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1274A1
Enter key:
KB Post Contributed By Scott Wehrly:

Scott Wehrly is currently working on .Net web applications for the gaming industry. Scott is a former employee of Prestwood Software (he was a Development Manager). Scott's specialties include C#, ASP.Net, MSSQL Server 2005, Delphi, SQL databases, C++, C, and Windows programming in general. When time allows, he participates in this online community.

Visit Profile

 KB Article #100017 Counter
Since 4/2/2008
Sales Website: Or visit our legacy sales site:

©1995-2021 Prestwood IT Solutions.   [Security & Privacy]