Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Parse TAB Delimited Files in Delphi

and Display in the TStringGrid Delphi control

By Zarko Gajic, About.com

Parse Tab Delimited file in Delphi

Parse Tab Delimited file in Delphi

TAB delimited text (ASCII) files contain lines of code with each chunk (column) of data (string) separated by the TAB character.

Most database and spreadsheet programs are able to read or save data in a delimited format.

Displaying TAB delimited Files in a TStringGrid Control

To display the TAB delimited files in a TStringGrid Delphi control use the next idea:
  1. Create a TStringList object ("sl" in the code below) to hold the TAB delimited file,
  2. Use the LoadFromFile method of the TStringList to fill the list with the lines of text in a specified TAB delimited file,
  3. Use another instance of the TStringList ("slRow" in the code below) to process each TAB separated line in "sl",
  4. Use the DelimitedText property to set all the strings in "slRow" object in a single string. The
  5. Set the Delimiter property to #9 which is the character control sequence for the TAB character.
  6. For each string chunk in "slRow" write the string in the TStringGrid a cell.
Note: Delphi's TStringGrid control represents a grid control designed to simplify the handling of strings and associated objects.

Here's the code:

procedure TForm1.ProcessTabDelimitedFile(const fileName: TFileName) ;
var
  sl, slRow : TStringList;
  line, col : integer;
begin
  //will load the TAB delimited TXT here
  sl := TStringList.Create;
  //will process each TAB delimited line here
  slRow := TStringList.Create;
  slRow.Delimiter := #9; //TAB
  try
    //load the tab delimited txt file
    sl.LoadFromFile(fileName) ;
    StringGrid1.RowCount := sl.Count;

    //for each tab delimited line
    for line := 0 to -1 + sl.Count do
    begin
      //"load" the line into a stringlist
      slRow.DelimitedText := sl[line];
      for col := 0 to -1 + slRow.Count do
      begin
        StringGrid1.Cells[col,line] := slRow[col];
      end;
    end;
  finally
    slRow.Free;
    sl.Free;
  end;
end;
Delphi tips navigator:
» Implement the On Item Checked Event for the TListView Delphi control
« Convert RGB to TColor or How to Get More TColor values for Delphi
More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2007 Tips
  7. How to Parse TAB Delimited Files in Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.