« Return to Thread: My Story + Small Request

Re: My Story + Small Request

by Berkant Atay :: Rate this Message:

Reply to Author | View in Thread

Now I have the time to help you more.

In these examples, I first demonstrate the exit button. The second one shows how to create and write something on an SD card file, then read it, and then create a stream file (Palm internal memory file), and writing records on it, kind of database demo... The second program chunk retrieves what the first program has put in the stream file. However, you have to refer to Plua.doc for details about files, records, and tables.

1. FIRST THE EXIT BUTTON:  **********************

screen.moveto(100,100)
buttonexit=gui.button("Quit")
while true do
ev,id=gui.event()
if ev==appStop then break end
if ev==ctlSelect and id==buttonexit then os.exit() end
end

*************************************************

2. NOW SOME FILE OPERATIONS: ********************

-- ** Writes to VFS and stream files  **
-- First, you can save items in a VFS file. Remember your SD card is vfs0, ie "vfs zero"..!


print() print() print("Writing and reading to a VFS file")
os.remove("vfs0:/PALM/Launcher/myfile.who")
f=io.open("vfs0:/PALM/Launcher/myfile.who", "w")
a=math.p --just a numeric example..
f:write("pencil","ruler")
f:write("compass",a)
f:close()
f=io.open("vfs0:/PALM/Launcher/myfile.who","r")
s=f:read()
print(s)
f:close()
os.remove("vfs0:/PALM/Launcher/myfile.who") --we don't need this file anymore..
print() print("Press any key...")
gui.event() --this makes the OS wait for any event

-- Creating and putting records in a stream file

print("...now writing records to a RAM file...")
name=1 age=2 gender=3 value=4
person={}
for n=1,3 do person[n]={} end  -- we defined a matrix with two dimensions

person[1][name]="BERKANT"
person[1][age]=41
person[1][gender]=1
person[1][value]=100

person[2][name]="TUBA"
person[2][age]=36
person[2][gender]=0
person[2][value]=90

person[3][name]="YIGIT"
person[3][age]=9
person[3][gender]=1
person[3][value]=80  -- you can find other ways to enter these data..

-- first remove the existing file

os.remove("person")

-- check for packing string format in Plua.doc

-- write to file in RAM

f=io.open("db:/person", "r+")
for i=1,3 do
data = bin.pack("SBBB", person[i])
index = f:createrec(string.len(data))
f:openrec(index)
f:write(data)
end
f:close()

print("Finished writing to file.")
print("Press Home key to quit...")
gui.main() -- this waits for the "home" key


*************************************************************

3. NOW, THIS FILE READS WHAT THE ABOVE CODE HAS WRITTEN: ****


-- **Retrieves previously written files **

person={}
f,recs =io.open("db:/person", "r")
for i=1,recs do
person[i]={}
end

name=1
age=2
gender=3
value=4

-- note that arrays are indexed starting from 1!!!
-- but file recs index starts with 0 !

for i=0,recs-1 do
f:openrec(i)
data = f:read("*a")
table=bin.unpack("SBBB", data)
person[i+1][name]=table[1]
person[i+1][age]=table[2]
person[i+1][gender]=table[3]
person[i+1][value]=table[4]
end
f:close()

for i=1,recs do
print(person[i][name])
print(person[i][age])
print(person[i][gender])
print(person[i][value])
end

gui.main()

*********************************************************************

Hope you are progressing fast...

 « Return to Thread: My Story + Small Request