Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a text file

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I need to use my Delphi application to create a text file (which I haven't done before!) and I'm not sure how to start.

I have all the information to go in the file, but it has to be structured:

First field 10 characters
Second field 5 characters
Third field 25 characters
etc.

so if my first row of data for field1 is only 6 characters I need to fill in the other 4 with blanks, if field2 is blank then skip those five characters, and then fill in field3:

1111111111222223333333333333333333333333
Leslie 123 Main St.

Can anyone point me in the right direction to the best way of creating a file with structure? Thanks for any info.

Leslie
 
Leslie,

You're looking for the Format() function in the SysUtils unit. It lets you specify the width of the output string, as well as control padding, leading zeros, and so on.

Hope this helps...

-- Lance
 
Do I have to format each field or can I have a "standard" format for the entire record that is applied to every record?


Leslie
landrews@metrocourt.state.nm.us

There are 10 types of people in the world -
those who understand binary
and
those who don't!
 
You would need to format each line individually. The easiest way is to create a Stringlist and add your data to it in the correct format. In the following example you will see how to set each felds length (I have used edit boxes as the input data) and how to add it as a line to the stringlist.
Code:
var
  MyList : TStringList;
  A : Array[1..3] of String;
begin
  MyList := TStringList.Create;
  A[1] := Format('%-10s',[Edit1.Text]);
  A[2] := Format('%-5s',[Edit2.Text]);
  A[3] := Format('%-25s',[Edit3.Text]);
  MyList.Add(A[1] + A[2] + A[3]);
  MyList.SaveToFile('C:\MyFilePath\MyFileName.txt');
  MyList.Free;
end;
Of course you can then place the code into a loop which meets your specific conditions to add as many lines as you require to the stringlist.

To load the text file back into a stringlist, you can use the Loadfromfile command. i.e
Code:
  MyList.LoadFromFile('C:\MyFilePath\MyFileName.txt');
[code]


[i]When your feeling down and your resistance is low, light another cigarette and let yourself go[/i]    [rockband]
 
Ok, I started playing around with the Format function yesterday. Thanks for the suggestions. I'm trying to use the '.' to truncate the string if it's too long, where do I put it?

Here's a more detailed description of what I'm trying to accomplish:

Once the jurors have completed the term of duty, my program generates payment information. Instead of printing a report from my system and manually entering the information into the payment system, I am creating a text file for FTP to the payment system. The first 36 bytes of each record are the same. The first record is a header record that I have to fill in specific information and all the records under it are detailed records for payment.

I currently have a process set up to export this information into an Excel spreadsheet. So I have all the information I need for the detail records, I just need to get them into the text file for transmittal.

Thanks!

leslie
 
Maybe someone can help me figure this out. I have all my detail information calculated, but the header record needs a total of all the detailed records and I can't figure out the best way to get the total before the details!

Thanks for any insight!

leslie
 
If you use the StringList approach, suggested by EricDraven, then it is quite easy.

Write all the details to the StringList (suggested name of MyList).

Then MyList.Count contains the number of records which you then add into your Header Details string.

You can either insert the header details into the StringList using something like
Code:
  ...
  MyList.Insert ( 0, HeaderDetails );
  ...

or when you come to write out the StringList to your file you start by creating and outputting the record containing the value of MyList.Count.

Andrew
 
This is for juror payments and what I need is the total dollar amount of the details:


Joe Blow 123 Main St 65.00
Jane Doe 321 S. Main st 65.00


Total = 130.00

not the count of the records. I'm still working on the best way to approach this so all suggestions are welcome. I think I am going to use the String List, but I'm not quite sure how yet!

Thanks for the suggestion Andrew! I'll be working on this again tomorrow, I'll post back with any inspirations or more advice needed.

Leslie
 
Leslie, not sure why you can't just accumulate the total as you go while adding strings to your list, but here is an alternative to play with:

You can exploit the Objects property of the TStringList by storing the your amounts in it.

Store amounts:
Multiply by 100 to convert to pennies as an integer, cast it to an object and store the value for each line of data.

Retrieve amounts and accumulate:
Loop thru the list and retrieve the amounts by casting the object back to an integer and accumulate the sum. Finally divide by 100 to get the total back to dollars and cents.

But, as I said, if you can do that, you should be able to simply accumulate the amounts as you go.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top