Text files [examples]

Read a file into a text string
FILETOSTR
myString = filetostr("myfile.txt")

https://www.lianja.com/doc/index.php/FILETOSTR()


Write a text string out to a file
STRTOFILE
myString = "Hello World"
 
// Create file if it does not exist, overwrite if it does
nBytes = strtofile(mystring, "myfile.txt")
 
// Append to file's previous contents
nBytes = strtofile(mystring, "myfile.txt",.T.)
 
// Create file if it does not exist, overwrite if it does
nBytes = strtofile(mystring, "myfile.txt",0)
 
// Append to file's previous contents
nBytes = strtofile(mystring, "myfile.txt",1)
 
// Create file if it does not exist, overwrite if it does.  Include Unicode BOM
nBytes = strtofile(mystring, "myfile.txt",2)
 
// Create file if it does not exist, overwrite if it does.  Include UTF-8 BOM
nBytes = strtofile(mystring, "myfile.txt",4)

https://www.lianja.com/doc/index.php/STRTOFILE()


Return the value from a key in a specified section of an ‘ini’ file
INI_GET
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
key1value = ini_get('global','key1','unset','test.ini',.T.)

https://www.lianja.com/doc/index.php/INI_GET()


Return all the key/value pairs from a specified section of an ‘ini’ file
INI_GETSECTION
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
obj_global = ini_getsection('global','test.ini')
? obj_global.key3
This is $(key2) and $(key1)

https://www.lianja.com/doc/index.php/INI_GETSECTION()


Set the value of a key in a specified section of an ‘ini’ file
INI_SET
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
ini_set('global','key1','new value','test.ini')

https://www.lianja.com/doc/index.php/INI_SET()


Set all the key/value pairs of a specified section of an ‘ini’ file
INI_SETSECTION
# test.ini
# comment lines are preceeded by a '#' 
; or a ';'
#
# "Sections" are enclosed in [ and ]
[global]
; key/value pairs are contained in the sections and are written like this
 	key1 = value
 	key2 = value2
; You can include macros in the key/value pairs.
 	key3 = This is $(key2) and $(key1)
# end of test.ini
obj_global = ini_getsection('global','test.ini')
? obj_global.key3
This is $(key2) and $(key1)
obj_global.key3 = 'value3'
ini_setsection(obj_global,'global','test.ini')
? ini_get('global','key3','','test.ini')
value3

https://www.lianja.com/doc/index.php/INI_SETSECTION()


Enables or disable the inclusion of field name headings in COPY TO … TYPE CSV exports
SET CSVHEADING
open database southwind
use products
set csvheading off
copy to exportproducts type CSV delimited with '|'
type exportproducts.csv

https://www.lianja.com/doc/index.php/SET_CSVHEADING


Copy a memo field into a file
COPY MEMO
seek "JimL"
do while emp_code = "JimL"
    copy memo notes to comments additive
    skip
enddo

https://www.lianja.com/doc/index.php/COPY_MEMO


Capture output text in a file
SET ALTERNATE
set alternate to alt
? "Write this text to file"
set alternate off
? "Don’t write this text to file"
?
set console off
? "write this to file to the file, not to the screen"
close alternate
set console on
return

https://www.lianja.com/doc/index.php/SET_ALTERNATE


Enable/disable text merging, specify text merging target or specify text merge delimiting characters
SET TEXTMERGE
open database southwind
use example
set textmerge to balance.txt
set textmerge delimiters to "{{","}}"
set textmerge on
go top
do while balance > 0
    text noshow
    Date: {{date()}}
    Name: {{last_name}}  {{first_name}}
 
    Account Number  :  {{account_no}}
    Current Balance  :   {{balance}}
    Credit Limit:        :   {{limit}}
    endtext
    skip
enddo
set textmerge off
set textmerge to

https://www.lianja.com/doc/index.php/SET_TEXTMERGE


Evaluate a character string, merging any delimited expressions
TEXTMERGE
fd = fcreate("template.txt")
fputs(fd,"Dear <<trim(first_name)>>,")
fputs(fd, "Thank you for your email of <<date()>>.")
fputs(fd, "We will be back in contact shortly.")
fputs(fd, "Best regards,")
fputs(fd, "<<username()>>")
fclose(fd)
 
m_contents = filetostr("template.txt")
open database southwind
use example in 0 current
 
?textmerge(m_contents)

https://www.lianja.com/doc/index.php/TEXTMERGE()


Write a string with ‘C’ style picture formatting to a file opened with the specified file pointer
FPRINTF

 

fp=fcreate('fprintf.txt')
// When %s is specified, the corresponding argument is converted to 
// character format (similar to specifying etos()).
// Widths correspond to the default values, e.g. numerics are 10
fprintf(fp,'It is %s, %s to be more precise\n',year(date()),datetime())
fprintf(fp,'The value of pi is %s\n',pi())
fprintf(fp,'They cost %s per %s\n',$99,100)
fprintf(fp,'Logicals can be %s or %s\n',.T.,.F.)
// Formatting characters can contain a width, which will left pad with spaces 
fprintf(fp,'Right-justify and pad left: %10s this\n','Like')
// Left justify by placing a '-' directly following the '%' character 
fprintf(fp,'Left-justify and pad right: %-10s this\n','Like')
// %d is for numerics
fprintf(fp,'It is %d\n',year(date()))
// %t and %T are for formating datetime data types.
fprintf(fp,'It is %d, %t to be more precise\n',year(date()),datetime())
fprintf(fp,'It is %d, %T to be even more precise\n',year(date()),datetime())
// %f is for floating point numerics
fprintf(fp,'The value of pi is %f\n',pi())
// Decimal places can also be specified for floating point numerics (%f)
fprintf(fp,'The value of pi to two decimal places is %4.2f\n',pi())
// %y is for formatting currency data types
fprintf(fp,'They cost %y per %d\n',$99,100)
fprintf(fp,'They cost %y per %d\n',$99,1000)
fprintf(fp,'They cost %y per %d\n',$99,10000)
//%l and %L are for formatting logical datatypes.
fprintf(fp,'Logicals can be %l or %l\n',.T.,.F.)
fprintf(fp,'Logicals can also be %L or %L\n',.T.,.F.)
fclose(fp)
// Contents of fprintf.txt
It is       2009, 11/11/2009 11:41:51 AM to be more precise
The value of pi is  3.1415926
They cost $99.0000 per        100
Logicals can be True or False
Right-justify and pad left:       Like this
Left-justify and pad right: Like       this
It is 2009
It is 2009, 11/11/2009 11:41:51 AM to be more precise
It is 2009, Wednesday November 11 2009 11:41:51 to be even more precise
The value of pi is 3.141593
The value of pi to two decimal places is 3.14
They cost $99.0000 per 100
They cost $99.0000 per 1000
They cost $99.0000 per 10000
Logicals can be True or False
Logicals can also be Yes or No

https://www.lianja.com/doc/index.php/FPRINTF()


Import

use example
// Standard data file
append from custsdf sdf
// Fixed length fields
append from custfix fixed
// Delimited (',' separated, "" around character fields)
append from custdel delimited
// Delimited with blank (single space separates fields)
append from custblank delimited with blank
// Delimited with  (',' separated,  around character fields)
append from custpipe delimited with |
// Microsoft Excel compatible comma-separated
append from custcsv csv

Export

use example
// Standard data file
copy to custsdf sdf
// Fixed length fields
copy to custfix fixed
// Delimited (',' separated, "" around character fields)
copy to custdel delimited
// Delimited with blank (single space separates fields)
copy to custblank delimited with blank
// Delimited with  (',' separated,  around character fields)
copy to custpipe delimited with |
// Microsoft Excel compatible comma-separated
copy to custcsv csv
select * from shippers to file shiptxt

https://www.lianja.com/doc/index.php/Importing_and_Exporting_Data


Output lines of text

open database southwind
use example
set textmerge to balance.txt
set textmerge delimiters to "{{","}}"
set textmerge on
go top
do while balance > 0
    \Date: {{date()}}
    \\Name: {{last_name}}  {{first_name}}
 
    \Account Number  :  {{account_no}}
    \Current Balance  :   {{balance}}
    \Credit Limit:        :   {{limit}}
    \
    skip
enddo
set textmerge off
set textmerge to

https://www.lianja.com/doc/index.php/Single_Textmerge


Output lines of text

open database southwind
use example
set textmerge to balance.txt
set textmerge delimiters to "{{","}}"
set textmerge on
go top
do while balance > 0
    \Date: {{date()}}
    \\Name: {{last_name}}  {{first_name}}
 
    \Account Number  :  {{account_no}}
    \Current Balance  :   {{balance}}
    \Credit Limit:        :   {{limit}}
    \
    skip
enddo
set textmerge off
set textmerge to

https://www.lianja.com/doc/index.php/Double_Textmerge


open database southwind
use example
fp=fcreate("names.txt")
count=0
do while not eof()
    count = count + fputs(fp,trim(last_name) + ", "+trim(first_name))
    skip
enddo
fclose(fp)
echo str(count,5) + " bytes written.\n"
 
fp = fopen("names.txt")
count = 0
do while not feof(fp)
    if left(fgets(fp),5) = "Smith"
        ++count
    endif
enddo
fclose(fp)
echo str(count,5) + " Smiths found.\n"
close databases

https://www.lianja.com/doc/index.php/Working_with_Files_and_File_Systems_in_Recital


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.