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!

Writing records to a text file 2

Status
Not open for further replies.

ejhemler

Programmer
Aug 30, 2002
68
US
Is there a way to export records from a foxpro table into a text file? I need each record to be a new line in the text file. The fields also need to be in the same order that they are in the record.
 
Also, Check out the STRTOFILE() function. You can add desired spacing, etc. and it works good in some instances.

Jim Osieczonek
Delta Business Group, LLC
 
the strtofile() function worked. but i need to add a character return at the end of the string so each string is added to the text file on a new line. is there a way to do this?

Thanks,
Erik
 
jimoo has a good idea with STRTOFILE(). Use that if COPY TO ... DELIMITED WITH ... is not what you need.

I must admit that up to now I had been doing it the old way, as I also needed to format and display the data slightly different from what appears in the record itself:

[/code]* This example uses low-level file access
private nHandle, nReply, cReply
nHandle=FOPEN(in_filename,1)
SCAN && FOR cMyFilter
* FPUTS() adds a CR/LF {CHR(13)+CHR(10)}
* to the end of the written string
* Field 1 is C type
* Field 2 is N type
* Field 3 is D type
* Field 4 is L type
nReply=FPUTS(nHandle,UPPER(EVAL(FIELD(1)));
+STR(EVAL(FIELD(2),11,2));
+DTOS(EVAL(FIELD(3)));
+IIF(EVAL(FIELD(4)),"T","F"))
ENDSCAN
IF FCLOSE(nHandle)
cReply="OK"
ENDIF[/code]
 


An alternative would be "How to create a file and write to it." Using Windows Scripting, see faq184-4140.


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
A correction to my earlier post, I had used FOPEN() when I should have used FCREATE().

Nice to see there are so many ways to do the same thing.
 
_FH = fcreate("mytxtfile.txt")
if _FH < 0 then
return
endif
select a
use yourtable
_NumberOfFields = fcount()
scan
*:Ciclo para formar la cadena con los datos de los campos del registro actual
_FieldString = &quot;&quot;
For _X = 1 TO _NumberOfFields
*:Obteniendo el nombre del campo segun el su orden en la tabla
_Campo = FIELD(_X)
*:Verificando el typo de dato que guardael campo
_VariableType = TYPE(FIELD(_X))

*:Convirtiendo cualquier tipo de dato que sea en caracter para poder unirlo con los demas
Do Case
Case _VariableType = [N]
_VariableChar = Alltrim(Str(&_Campo))
Case _VariableType = [D]
_VariableChar = DTOC(&_Campo)
Case _VariableType = [L]
_VariableChar = &_Campo
_VariableChar = IIF(_VariableChar,[.T.],[.F.])
OtherWise
_VariableChar = &_Campo
EndCase
*:Uniendo los datos del campo en una cadena de caracteres
_FieldString = _FieldString + IIF(_X=_NumberOfFields,_VariableChar,_VariableChar + [·])
EndFor
*:Escribiendo los datos del registro al archivo creado
=FPUTS(_FH,_FieldString)
ENDSCAN
FCLOSE(_fh)
MODI COMM MYTXTFILE.TXT
Return
 
Mike, send me an e-mail (address in on my website) and I'll send you a few more scripting host samples if you like. You can add them to your FAQ.


Jim

Jim Osieczonek
Delta Business Group, LLC
 
Another possible solution for formatting the data is to create a report with the formatted data and 'print' it using the 'to file' clause.

 
with a file of that size you wont have to do this but if you whant to put a lot of information into the text file you shold put this line after fputs:

= FFLUSH(_fh)
 
use items
list to txt && output to a file txt.txt
To customize your reports you can use
set prin to txt.txt
set devi to print
go top
do while !eof()
@prow()+1 , 1 say &quot;Item name : &quot; + item_name
.
.
.
skip
enddo
set prin to
set devi to screen



Its me : ALEX

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top