Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Padding a text file with spaces the same length as empty fields.
Fri, Mar 10 2006 12:41 AMPermanent Link

Patrick

I'm trying to generate a text file with lines of a fixed length (375 char)
but when I write the individual field values to the textfile it's not including the length
of the
fields in that text file, each of the field values have to start and end at specific chars.
My question is how would I be able to pad the spaces into my text file so that for every
field that is blank it writes spaces for the length of that field? So that my final line
length is always 375.


this is essentially what I was trying to use to write the text file.

... for i := 0 to (tbSampleMASTER.RecordCount - 1) do
 begin              // Beginning of Record String
   WriteLn(SampleTxtFile,
   tbSampleMaster.FieldByName('B_RECORD_NAME').AsString+                  
   tbSampleMaster.FieldByName('B_REPORT_TYPE').AsString+                     
   tbSampleMaster.FieldByName('B_TRANSACTION_NUMBER').AsString+         
   tbSampleMaster.FieldByName('B_LAB_SAMPLE_NUMBER').AsString+              
   tbSampleMaster.FieldByName('B_STATE_LAB_SAMPLE_NUMBER').AsString+         
   tbSampleMaster.FieldByName('B_WATER_SYSTEM_NUMBER').AsString+  ...
Fri, Mar 10 2006 3:24 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Patrick


Four suggestions

1. Change the underlying database to use fixed length character fields
2. Use Delphi's Format function
3. Write a small function yourself
4. Something like Copy(tbSampleMaster.FieldByName('B_RECORD_NAME').AsString+StringOfChar(' ',SizeWanted)    ,1,SizeWanted)

4 is what I'd use in 3

Roy Lambert
Fri, Mar 10 2006 4:22 AMPermanent Link

"Iztok Lajovic"
Patrick.

>
> this is essentially what I was trying to use to write the text file.
>
> .. for i := 0 to (tbSampleMASTER.RecordCount - 1) do
>  begin              // Beginning of Record String
>    WriteLn(SampleTxtFile,
>    tbSampleMaster.FieldByName('B_RECORD_NAME').AsString+
>    tbSampleMaster.FieldByName('B_REPORT_TYPE').AsString+
>    tbSampleMaster.FieldByName('B_TRANSACTION_NUMBER').AsString+
>    tbSampleMaster.FieldByName('B_LAB_SAMPLE_NUMBER').AsString+
>    tbSampleMaster.FieldByName('B_STATE_LAB_SAMPLE_NUMBER').AsString+
>    tbSampleMaster.FieldByName('B_WATER_SYSTEM_NUMBER').AsString+  ...
>

at the end of each field simply add desired length of output field. The
syntax is as follows (see Write procedure for text files in Delphi Help)

writeln(SampleTextFile, field1[:<length>[:<decimal places]] [,
field2[:<length>[:<decimal places]]...]);  // colon is separator

Length and decimal places can be defined by variable, of course. For
example:
...., SampleTable.fieldByName('street').asString + ':' +
intToStr(StreetLength), ...

if there is a blank field with desired length of 20 characters then for that
field specify:    ... , ' ':20, ...

Iztok Lajovic

Fri, Mar 10 2006 10:23 AMPermanent Link

"Allan Brocklehurst"
Patrick;

here is what I did

uses
 StrUtils,   // Unit containing the StuffString

var   ADTICFSND_REC :string
 Offset ,Stringlength  : Integer
begin
ADTICFSND_REC := '';
    ADTICFSND_REC := PadRight(ADTICFSND_REC, 1073, ' ');
Offset := 10;
Stringlength := 13
ADTICFSND_REC := StuffString(ADTICFSND_REC, Offset, Stringlength, 'ASTRING
Value');
end;

Allan

"Iztok Lajovic" <iztok.lajovic@amis.net> wrote in message
news:C2EA3DB7-7C55-4F14-BF6B-8FBBD8C3C721@news.elevatesoft.com...
> Patrick.
>
> >
> > this is essentially what I was trying to use to write the text file.
> >
> > .. for i := 0 to (tbSampleMASTER.RecordCount - 1) do
> >  begin              // Beginning of Record String
> >    WriteLn(SampleTxtFile,
> >    tbSampleMaster.FieldByName('B_RECORD_NAME').AsString+
> >    tbSampleMaster.FieldByName('B_REPORT_TYPE').AsString+
> >    tbSampleMaster.FieldByName('B_TRANSACTION_NUMBER').AsString+
> >    tbSampleMaster.FieldByName('B_LAB_SAMPLE_NUMBER').AsString+
> >    tbSampleMaster.FieldByName('B_STATE_LAB_SAMPLE_NUMBER').AsString+
> >    tbSampleMaster.FieldByName('B_WATER_SYSTEM_NUMBER').AsString+  ...
> >
>
> at the end of each field simply add desired length of output field. The
> syntax is as follows (see Write procedure for text files in Delphi Help)
>
> writeln(SampleTextFile, field1[:<length>[:<decimal places]] [,
> field2[:<length>[:<decimal places]]...]);  // colon is separator
>
> Length and decimal places can be defined by variable, of course. For
> example:
> ..., SampleTable.fieldByName('street').asString + ':' +
> intToStr(StreetLength), ...
>
> if there is a blank field with desired length of 20 characters then for
that
> field specify:    ... , ' ':20, ...
>
> Iztok Lajovic
>
>

Fri, Mar 10 2006 12:46 PMPermanent Link

Patrick
Roy Lambert <roy.lambert@skynet.co.uk> wrote:

Patrick


Four suggestions

1. Change the underlying database to use fixed length character fields
2. Use Delphi's Format function
3. Write a small function yourself
4. Something like
Copy(tbSampleMaster.FieldByName('B_RECORD_NAME').AsString+StringOfChar(' ',SizeWanted)  
,1,SizeWanted)

4 is what I'd use in 3

Roy Lambert

Thanks Guys,
I ended up using this on each record in the table:

if not tbSampleMaster.FieldByName('B_COMPLIANCE_INDICATOR').IsNull then
     begin
     line := line+tbSampleMaster.FieldByName('B_COMPLIANCE_INDICATOR').AsString+
     StringOfChar('
',1-length(tbSampleMaster.FieldByName('B_COMPLIANCE_INDICATOR').AsString));
     end else
     line := line+StringOfChar(' ',1);

because the fields were not always null, if they had a value, and that value was < the
field size then I needed to pad the individual fields as well, making sure that each new
field in the record starts at the exact same char() in the string/line.

Thanks again for getting me headed in the right direction.
-Patrick
Fri, Mar 10 2006 12:51 PMPermanent Link

"R. Tipton"
Hi Patrick

I emulated the dBase strzero() with the Delphi Function
FormatFloat() by doing........

Var sNumber: String;

sNumber := FormatFloat('00000',2);

That populates sNumber with '00002'

If thats what your after
Rita


Image