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

Writing records to text file 1

Status
Not open for further replies.

FoxFool

Programmer
Apr 15, 2004
10
0
0
US
Hi all. I was wondering if there is an easy way to write a table to a text file the looks like a label.
ex:
MYTABLE.DBF
name ADDRESS CITY,STATEZIP
Mr. Jones 1111 Main New York, NY 11111

I need to put all records in mytable.dbf into text file like so:
Mr. Jones
1111 Main
New York, NY 11111
(empty line, serves as record separator)
next record

I'm kind of new to VFP, so I've tried the label designer, but when I write it to file filename ascii, the end of each page hase blank spaces. I need the spacing to be consistent throughout the text file. I've also tried to insert chr(13)+chr(10) after each field then copy to xxx.txt type delimited with space, but I get '' preceeding every field. Maybe filetostr(). Hopefully this makes sense. Any tips would be greatly appreciated. Thanks!
 
There are a couple ways you could do it. Since it appears you need a little more control over the data than the "Copy To" command, I suggest the STRTOFILE function. Here is the help:

* STRTOFILE(cExpression, cFileName [, lAdditive | nFlag])


Here is an example:

Code:
* define carriage return & line feed
lcCRLF = CHR(13)+CHR(10)

STRTOFILE([Dear Mom]+lcCRLF+lcCRLF, [c:\deleteme.txt], .F.)
STRTOFILE([How are you.  I am fine.] + lcCRLF, [c:\deleteme.txt], .T.)
STRTOFILE([Your Dog], [c:\deleteme.txt], .T.)

MODIFY FILE c:\deleteme.txt

Copy the code into a prg and execute it to see it work in action.




Jim Osieczonek
Delta Business Group, LLC
 
For true format control over output, you might want to consider HTML. There's an example of DBF to HTML specifically for Excel, but shows how any type of HTML file can be written...

Export a Formatted Table to Excel using HTML faq184-4704

Brian
 
Hi

DOnt worry that you dont know. You can quickly learn.

Let me put the first lesson as easy as possible from my words.

1. In the command window of VFP write the code.. and press enter
CREATE REPORT myList FROM myTable FORM

Note.. FORM and FROM correctly spelled.
FORM will make the listing as you want - as in label
COLUMN or no form present will make that columnor.

2. Now.. type the following and do the rest in command window.
MODI REPO myList

a. In the resulting window, you select the field names appearing on the middle part of the report .... one by one and delete them. This is because you dont want the report to contain the field names. You can also delete the 'Page' box, if you dont need it.

b. Also on the band where it is wrotten 'Detail', click and hold the mouse on that band and pull it down to insert as much extra width you may want between the records.

c. Now close the designer and save the myReport form.

(d. You can also highlight the boxes and move them to which ever position you want. But ignore it for your first report.)

3. Now your report format myReport is ready.
In the command window type the following and press enter to get your report..

REPORT FORM myReport TO myFile.txt ASCII

Your report is ready as a text file.. myFile.txt

Now you can manipulate whatever you want using text editor..
*************************************************

Now to put the whole thing in simple quick code.. using a cursor....

** 1. Select wanted records in a cursor
SELECT name, address, city, state, zip, ;
SPACE(5) as BlankLine ;
FROM myTable INTO CURSOR temp ;
ORDER BY name

** You can ignore step 1, if you use the myTable in its natural order of data in the place of 'temp' in the following command.

** 2. Create a report on the fly.
CREATE REPORT myList FROM temp FORM

** 3. Process the report to a text file
REPORT FORM myList TO myTextFile ASCII

************************************************

The report form myList is saved in your directory and so you can reuse the report form.

Good luck
:)


____________________________________________
ramani - (Subramanian.G) :)
 
From Foxpro Dos - but still works

use {datafile}
goto top
set alte to [TMP.TMP]
set alte on
set cons off
do while .not. eof()
?name
?address
?trim(city)+[, ]+trim(state)+[ ]+zip
?
skip
endd
set alte off
set alte to
set cons on
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top