Text files – low level [examples]

Create an ASCII text file
FCREATE
use acc_log
scatter to flist
m_total=alen(flist)
fp=fcreate("new.txt")
if ferror()=-1
    dialog box "The file could not be created."
else
    for n=1 to m_total
        fwrite(fp,flist[n],80)
    next
endif
fclose(fp)
if ferror()=-1
    dialog box "The file could not be closed."
endif

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


Close an open text file
FCLOSE
use accounts
scatter to flist
m_total=alen(flist)
fp=fcreate("new.txt")
if ferror()=-1
    dialog box "The file could not be created."
else
    for n=1 to m_total
        fwrite(m->fp,flist[n],80)
    next
endif
fclose(fp)
if ferror()=-1
    dialog box "The file could not be closed."
endif

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


Read and return a line of text from an ASCII file
FGETS
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/FGETS()


Write a character string to an ASCII file
FPUTS
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/FPUTS()


Read and return a line of text from an ASCII file
FREAD
fp = fopen("names.txt")
?fread(fp,80)
Smith,Bill
fclose(fp)

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


Return a character string from a text file
FREADSTR
use accounts
fp=fopen("existing.txt")
if ferror()=-1
    dialog box "The file could not be opened."
else
    string = freadstr(fp,80)
    do while not empty(string)
        ? string
        string = freadstr(fp,80)
    enddo
endif
fclose(fp)
if ferror()=-1
    dialog box "The file could not be closed."
endif

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


Position to a byte offset within a text file
FSEEK
fp = fopen("names.txt")
string = fgets(fp)
fseek(fp, 1024,0)

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


Write a character expression to an ASCII file
FWRITE
fp=fcreate("time.txt")
if ferror()=-1
    dialog box "The file could not be created."
else
    fwrite(fp,timestamp())
endif
fclose(fp)
if ferror()=-1
    dialog box "The file could not be closed."
endif

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


Open an existing ASCII text file or http URL
FOPEN
use accounts
fp=fopen("existing.file")
if ferror()=-1
    dialog box "The file could not be opened."
else
    string = fgets(fp)
    do while not empty(string)
        ? string
        string = fgets(fp)
    enddo
    ?
endif
fclose(fp)
if ferror()=-1
    dialog box "The file could not be closed."
endif  
 
// http URL example
<!-- twitter.rsp -->
<html>
<body>
<%
fp=fopen("http://twitter.com/recitalsoftware")
do while not feof(fp)
    ? fgets(fp)
enddo
fclose(fp)
%>
</body>
</html>

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


Check if the file pointer is at the end of an ASCII file
FEOF
open database southwind
use shippers
copy to ship.txt type csv
fd=fopen("ship.txt")
 
do while .t.
	cGetLine = fgets(fd)
	if feof(fd)
		exit
	else
		// process
	endif
enddo
fclose(fd)

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


Status of the last text-file operation
FERROR
use accounts
fp=fopen("existing.file")
if ferror()=-1
    dialog box "The file could not be opened."
else
    string = freadstr(fp,80)
    do while not empty(string)
        ? string
        string = freadstr(fp,80)
    enddo
    ?
    fclose(fp)
    if ferror()=-1
        dialog box "The file could not be closed."
    endif
endif

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


Flush internal buffer
FFLUSH
fwrite(fp,"Hello World")
fflush(fp)

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


 

Binary files [examples]

Open a binary file
BINOPEN
fd = binopen("file.obj")
count = binread(fd, 4)
count = bin2l(count)
count = l2bin(count + 1)
binseek(fd, 512, 0)
binwrite(fd, count)
binclose(fd)

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


Close a file opened for binary file access
BINCLOSE
fd = binopen("file.obj")
count = binread(fd, 4)
count = bin2l(count)
count = l2bin(count + 1)
binseek(fd, 512, 0)
binwrite(fd, count)
binclose(fd)

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


Create a binary file
BINCREATE
fd = bincreate("file.obj")
count = binread(fd, 4)
count = bin2l(count)
count = l2bin(count + 1)
binseek(fd, 512, 0)
binwrite(fd, count)
binclose(fd)

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


Position the file pointer in binary file
BINSEEK
fd = bincreate("file.obj")
count = binread(fd, 4)
count = bin2l(count)
count = l2bin(count + 1)
binseek(fd, 512, 0)
binwrite(fd, count)
binclose(fd)

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


Read a character string from a binary file
BINREAD
fd = bincreate("file.obj")
count = binread(fd, 4)
count = bin2l(count)
count = l2bin(count + 1)
binseek(fd, 512, 0)
binwrite(fd, count)
binclose(fd)

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


Write an expression to a binary file
BINWRITE
fd = binopen("file.obj")
count = binread(fd, 4)
count = bin2l(count)
count = l2bin(count + 1)
binseek(fd, 512, 0)
binwrite(fd, count)
binclose(fd)

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


Read an external binary file into an object field
OBJECTREAD
? objectread('images/brickwall.gif',WALLPAPER)
.T.

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


Size of object in an object field
OBJECTSIZE
open database southwind
use attachments
list objectsize(attachments)

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


Return object file extension
OBJECTTYPE
objectread('brickwall.gif',IMAGES)
? objecttype(IMAGES)
gif

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


Write an external binary file from an object field
OBJECTWRITE
? objectwrite("myicon.gif",ICONS)
.T.
? objectwrite("",PHOTO,.T.)
_0049e10001.gif

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


<%
open database southwind
use categories
scan
m_file = "__"+ltrim(str(recno()))+".jpg"
? ' <img src="'+objectwrite(m_file,picture,.t.)+'"><br>'
endscan
close databases
%>
<body bgcolor="#FFFFFF" text="#000000">
</body>

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


Low level

Q:
A need create a temporary file to use while the session is live and set a text field with this name to use macro on web client.
The command file=Lianja.evaluate(‘tmpnam()’) work on appbuilder, but not on webclient.
In fact a need create a POS.
So I am using catalogView to show items and I need create a grid with items to be saved on table.
But gadget grid dont work on web.
A:
Use a regular grid section, and make the header and/or footer invisible, and you can make it look all like one section, etc.
For storage: think JSON, and use SessionStorage



Low level

FSEEK() is actually moving the pointer. If I do the following I get the expected results except that BOF() doesn’t return True even before I move through the file.
Using FGETS(<fd>,0,1) appears to correctly return the current position:

Code:
open database southwind
use customers
copy to xxx.txt type csv
xxx=fopen("xxx.txt")
? xxx
? bof()
? fseek(xxx,0,1)
? fgets(xxx)
? fseek(xxx,0,1)
? fseek(xxx,0,0)
? fseek(xxx,0,1)

The FEOF() flag only gets set/reset after a read operation. As Dave says, the fseek(FileHandle,0,0) is returning you to the start of the file, it’s just that having been set, FEOF() won’t be unset until you do another read operation. Try something like this:

Code:
FileHandle = FOPEN(Lianja.Get("Gamesheet.GSHeader.txtGamesheet").text)

Do While .T.
  cGetline = fgets(FileHandle)
  If FEOF(FileHandle)
    exit
  else
    // process ...
  endif
ENDDO

fseek(FileHandle,0,0)  // Return to start of file

Do While .T.
  cGetline = fgets(FileHandle)
  If FEOF(FileHandle)
    exit
  else
    // process ...
  endif
ENDDO