Page 1 of 1

Writetable() questions

Posted: Wed Mar 17, 2010 5:59 pm
by Tarzin
Hello,

1) How can I know how many records have been written to a file with writetable() function ??

2) How can I add records to a file which have been written with writetable().
I've tried to use seek(1,#EOF). It seems that datas are recored but when I print them with nprint (data(47)), I got an error

I try to write data a first time to the file and then add datas a second time to the same file. I only success to get first datas recorded (46 by example), second datas (47 and more) are recorded too but can't be printed

Thanks for your help!

Re: Writetable() questions

Posted: Wed Mar 17, 2010 8:02 pm
by Tarzin
Tarzin wrote: 1) How can I know how many records have been written to a file with writetable() function ??
For this question, simple:
Print (ListItems(table))

Re: Writetable() questions

Posted: Wed Mar 17, 2010 11:01 pm
by airsoftsoftwair
You have to keep track of this on your own. I.e. simply write the number of tables at the start of the file and then call WriteTable() in a loop.

Re: Writetable() questions

Posted: Thu Mar 18, 2010 9:12 am
by Tarzin
So, first record should be my number of records in the table?

something like this table ={98,"data1", "data2", ....} where 98 is my numer of records?

I can't see how can I have somthing dynamic. 98 is in this case handly written. Can table={} handle variables?
Something like
c=Listitems(table)
table = {c, "data1", "data2", ....}

Re: Writetable() questions

Posted: Thu Mar 18, 2010 8:54 pm
by Allanon
Your sample code will work but ofcourse the value written to the file will be the number stored in 'c' and not the 'c' reference, but doesn't matter because you can load your table, modify wathever you want and then write your modified table overwriting the old one.

Re: Writetable() questions

Posted: Fri Mar 19, 2010 4:50 pm
by Tarzin
Need some help...
sorry for my beginner code, I'm learning

---

@VERSION 4,0
@APPTITLE "Writetable()"
@APPAUTHOR "Tarzin"
@APPVERSION "$VER : 0.01 - Mars 2010"
@APPDESCRIPTION "Writetable() function tests"

@DISPLAY {title="WriteTable() Function tests", x=#CENTER, y=#CENTER, width=640, height=480, color=$000000}

; num : number of records in the table
; dat1 : first serie of datas
; dat2 : second serie of datas
; num2 : total of records (dat1+dat2)

;open file and list fields
;
Function p_open()
OpenFile(1, "writetable.bin", #MODE_READ)
dat=ReadTable(1)
NPrint ("") NPrint (ListItems(dat),"Record(s) in table:") NPrint ("")

For i=0 To ListItems(dat)-1
NPrint ("Field",i,":",dat)
Next
EndFunction


Local num, num2 = 0
NPrint ("1) Opening & creating file writetable.bin ")
OpenFile(1,"writetable.bin", #MODE_WRITE)
dat1 ={num,"Data1","Data2","Data3","Data4"}
WriteTable(1,dat1)
CloseFile(1)
NPrint("") NPrint ("File writetable.bin created...") NPrint ("") NPrint ("")


NPrint ("2) Opening file writetable.bin...")
p_open()
CloseFile(1)
num=(ListItems(dat))
NPrint ("")


NPrint ("3) Opening file writetable.bin for the second time") NPrint ("")
OpenFile(1,"writetable.bin", #MODE_WRITE)
dat2 ={"Data5","Data6","Data7","Data8","Data9","Data10","Data11"}
num2=ListItems(dat2) NPrint (num2,"records to add") NPrint ("")
Seek(1,0)
dat3={num2+num}
NPrint("Writing writetable.bin Header...") NPrint("")
WriteTable (1,dat3)

NPrint ((num2+num),"fields to record")

NPrint("Writing dat2 to writetable.bin...") NPrint("")
Seek (1,#EOF)
WriteTable(1,dat2)
CloseFile(1)

NPrint("")NPrint ("4) Checking recorded datas...") NPrint ("")
p_open()

WaitLeftMouse()
CloseFile(1)
End

---
This code is not fully working. Table is written but datas are lost
How can I correct to have my 12 datas in writetable.bin file? (1 header data + 11 datas)