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

Field Description record for delimited output

Status
Not open for further replies.

mwhitman52

Technical User
Apr 10, 2003
13
US
Does anyone have a procedure or the method to write to a delimited file and include the field names as the first record?

Thank you
 
If you can use fixed length records, this will work:

SET HEAD ON
LIST TO SomeFile.TXT

If not, you will have to do something like:

Code:
USE MyTable

nHandle = FCREATE('OUTPUT.TXT')
IF nHandle < 0
   *... error routine
   CLOSE ALL
   RETURN
ENDIF

&&... write field name and pad with spaces, the length of field
nHowMany = AFIELDS(aFlds)
FOR nIndex = 1 TO nHowMany
   =FWRITE(nHandle, PADR(aFlds[nIndex, 1], aFlds[nIndex, 3]))  
NEXT

&&... write field data
nHowMany = AFIELDS(aFlds)
SCAN 
   STORE '' TO cTmpStr
   FOR nIndex = 1 TO nHowMany
      STORE '' TO cTmpData
      DO CASE 
         CASE aFlds[nIndex, 2] = 'N'
            cTmpData = PADL(ALLTRIM(STR(aFlds[nIndex, 1], ;
                  aFlds[nIndex, 3], aFlds[nIndex, 4])), aFlds[nIndex, 3])
            
         CASE aFlds[nIndex, 2] = 'D'
            cTmpData = ALLTRIM(DTOC(aFlds[nIndex, 1]))

         CASE    &&...   Any other data types you use here
            *...........
         
         OTHERWISE 
            cTmpData = PADR(ALLTRIM(aFlds[nIndex, 1]), aFlds[nIndex, 3])
         
      ENDCASE 
      cTmpStr = cTmpStr + cTmpData 
      =FWRITE(nHandle, cTmpData)  
   NEXT
ENDSCAN
=FCLOSE(nHandle)


Dave S.
[cheers]
 
If you can use commas as the delimiter, then this automatically writes the fields on the first line of the destination file, by default given the .CSV extension. Of course the COPY TO command will overwrite any data that was there previously.

COPY TO cfilename TYPE CSV
 
I have FoxPro 2.5, VFP 6 and VFP 7 so I can't check 2.6, 3 or 5, but somewhere between 2.5 and 6 TYPE CSV became a valid clause. It doesn't work version 2.5 or earlier though.


Dave S.
[cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top