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


Advertisements

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