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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output to Fixed width Text File

Status
Not open for further replies.

sinyce

IS-IT--Management
May 27, 2002
57
US
I have a database e.g.

NAME CNUM ADDRESS
John Doe 11212 New York, NY 10022
Mark Deer 22222 Orlando, Fl 33333

I want to output this to a fixed width Text file
with this parameter:

Field Length Position
Name 20 1
Cnum 5 21
Address 30 26

so for the above the output will look like this, (n.b. its the padding and calculating the number of pads that beats me on this one)

12345678901234567890123456789012345678901234
John Doe 11212New York, NY 10022
Mark Deer 22222Orlando, Fl 33333

(top row is for guidance only)



 
Try this:

use database
goto top
Hndl := fcreate('output.txt')
while !Eof()
fwrite(Hndl,PadR(cField1,20) + PadR(cField2,5) + ;
Str(nField3,8,2) + PadL(cField4,6) + Chr(13) + Chr(10))
skip
end
fClose(Hndl)
use

The PadR and PadL functions are very well documented in the guides.

Have Fun.
TonHu
 
Excellent ! Excellent! this was most helpful and efficient too. Thanks a million.

Sinyce
 
There is an inbuilt procedure for this: copy to ...


COPY TO
Export records to a new (.dbf) or ASCII file
--------------------------------------------------------
Syntax

COPY [FIELDS <idField list>] TO <xcFile>
[<scope>] [WHILE <lCondition>] [FOR <lCondition>]
[SDF | DELIMITED [WITH BLANK | <xcDelimiter>]]


Use the SDF option to use fixed length:

use database
copy to database.txt sdf

 
Yes, true, but the next question would be how to format separate fields the way he likes them better ;-)

Grtz, TonHu
 
TonHu:

I agree. The above data was just a sample, the real database has a few fields with dollar amount which would be right justified. In addition, SDF will output the data to match the field width in the database and not as specified.

Note: This file is then FTP'd to an AS/400 for some kinda batch processing.

Thanks again.

Sinyce
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top