Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 909347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:51:47+00:00 2026-05-15T16:51:47+00:00

I need to read data from a text file where the field lengths and

  • 0

I need to read data from a text file where the field lengths and record lengths are fixed. Fields are either zero padded or space padded, always appear in the same order and each record is terminated by a CRLF. The file can have one of three possible record types determined by the first character in the record.

So far I’ve create a base class for all record types and a child class for each record type.

type
  TRecordBase = class abstract
  public
    // Various common fields...
    function ToString: string; virtual; abstract;
    procedure Read(AString: string); virtual; abstract;
  end;

  TRecordType1 = class(TRecordBase)
  public
    //RecordType1 fields...
    function ToString: string; override;
    procedure Read(AString: string); override;
  end;

  TRecordType2 = class(TRecordBase)
  public
    //RecordType2 fields...
    function ToString: string; override;
    procedure Read(AString: string); override;
  end;

  TRecordType3 = class(TRecordBase)
  public
    //RecordType3 fields...
    function ToString: string; override;
    procedure Read(AString: string); override;
  end;

Then I simply read each line of the file as a string, determine its type from the first character, create the appropriate class instance and call Read.

The idea is that the Record classes can be used for both reading and writing to a string representation of the record. The Read procedure needs to break up a string and assign it to public fields.

I have two(or three) questions:

  • Is this a good approach to handle this type of file?
  • If so, what would your implementation of the Read procedure look like? (I’ve dealt with delimited files but this is my first encounter with fixed length fields)
  • If not, what approach would you take?

Update

Just thought I’d fill in some of the missing details. These record classes are essentially DTOs (data transfer objects). The fields are declared public and the only methods are for conversion to/from a string. The only data validation on the fields is the compiler’s type checking. Fields are converted to string in the required order using TStringBuilder.AppendFormat. This ensures fields are padded and/or truncated to the proper length.

I went with Rob’s suggestion to use Copy combined with the appropriate StrTo* for getting data from the string. I’ve also defined field positions and lengths as class constants, i.e.

const Field1Pos = 1;
const Field1Length = 1;
const Field2Pos = 2;
const Field2Length = 5;

The consts are a little easier to read than “magic numbers” in the calls to Copy.

Any other suggestions would be appreciated.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:51:47+00:00Added an answer on May 15, 2026 at 4:51 pm

    I’d change one thing: Replace the read procedure with an Read constructor, something like this:

    TRecordBase = class
    public
      constructor CreateFromText(Text:string);virtual;abstract;
    end;
    
    TRecordType1 = class(TRecordBase)
    public
      constructor CreateFromText(Text:string);override;
    end;
    

    Depending on what you do with your records this will save some typing and make code easier to read:

    var s:string; // string from stream or string-list
    if s[1] = 'X'then DoSomethingWith(TRecordType1.Create(s));
    

    Having a virtual constructor is also handy if the number of record types grows. You can do something like this:

    // Define an class type
    type TRecordBaseClass = class of TRecordBase;
    
    // Using Delphi 2010? Use a dictionary to register (FirstChar, TRecordBaseClass) paris
    var RecordClassDictionary = TDictionary<char, TRecordBaseClass>;
    
    // Init the dictionary like this:
    RecordClassDictionary.Add('1', TRecordType1);
    RecordClassDictionary.Add('2', TRecordType2);
    RecordClassDictionary.Add('3', TRecordType3);
    
    // And use it like this:
    var RecordBaseClass: TRecordBaseClass;
    for line in TextToParse do
      if RecordClassDictionary.TryGetValue(line[1], RecordBaseClass) then
         // Read the record, do something with the record
         DoSomethingWithTheRecord(RecordBaseClass.CreateFromText(line))
      else
         raise Exception.Create('Unkown record type.');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read the data from a file that can be either comma
I need to read text data from file, where there are different types of
I read text data from big file line by line. But I need to
I need to know how to read data from tabbed delimited text file in
I need to read data from XML to a List<>. The XML file contains
I need to read data from a legacy database file produced by Visual Basic
As part of an assignment, I need to read data from a binary file
I need to read some data from an input file and plot a graph
I need to read small sequences of data from a 3.7 GB file. The
Below is part of my code to read data from a text file, strip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.