LB Booster
Programming >> Liberty BASIC language >> editing data with timer
http://lbb.conforums.com/index.cgi?board=lblang&action=display&num=1436340986

editing data with timer
Post by hammerjit on Jul 8th, 2015, 07:36am

Hi...I am trying to edit a text file. The code will retrieve the record from text file then allow user to amend the first 2 fields. The third field suppose will calculate result. I have set my timer on and find it impossible to amend the first 2 field.

Can someone help me with this?

Eg : text.file

1, 10, 50

Code:

    nomainwin

    WindowWidth = 550
    WindowHeight = 410

    statictext #main.statictext1, "Record to Edit", 30, 86, 112, 20
    textbox #main.textbox2, 158, 81, 100, 25
    textbox #main.textbox3, 286, 81, 100, 25
    textbox #main.textbox4, 414, 81, 100, 25
    statictext #main.statictext5, "From (a)", 158, 51, 56, 20
    statictext #main.statictext7, "To (b)", 286, 51, 40, 20
    statictext #main.statictext8, "Total ( b-a x5)", 414, 51, 96, 20
    button #main.button9, "SAVE", [button9Click], UL, 158, 181, 42, 25
    open "untitled" for window as #main
    #main, "trapclose [quit1]"
    print #main, "font ms_sans_serif 0 16"


timer 100, [main.inputLoop]
[main.inputLoop]   'wait here for input event
OPEN "C:\test.txt" FOR INPUT AS #f
while eof(#f)=0
line input #f,l$
from$ = (word$(l$,1,",")):to$ = (word$(l$,2,",")):total$ = (word$(l$,3,","))
wend
close #f

    print #main.textbox2, from$
    print #main.textbox3, to$
    print #main.textbox4, total$

   PRINT #main.textbox2, "!contents? From$"
   PRINT #main.textbox3, "!contents? To$"
   PRINT #main.textbox4, "!contents? Total$"

    wait

[button9Click]   'Perform action for the button named 'button9'

OPEN "C:\test.txt" FOR OUTPUT AS #f
while eof(#f)=0
line input #f,l$
print #f, From$;", ";To$; ", ";Total$
wend
close #f

    wait

[quit1]
close #main


 


Re: editing data with timer
Post by Richard Russell on Jul 8th, 2015, 08:41am

on Jul 8th, 2015, 07:36am, hammerjit wrote:
I am trying to edit a text file... Can someone help me with this?

It looks to me as though you are trying to edit the file 'in situ', i.e. to make changes directly to an existing file. Whilst you can successfully do that with a random-access file, in which the records are all the same length, normally you cannot do so with a text file. That's because if a record increases in length there is no way to 'move' the rest of the file to accommodate the new record. Similarly if a record decreases in length there is no way to 'close up' the gap which is left.

So in general the way to 'edit' a text file is to create a completely new file which contains the modified text, and then at the end you can delete the original file and rename the new file to have the same name as the old file. To do that will require a significant modification to the structure of your program. If your application is a rare special case when you know that the edited data will always be the same length as the original data then 'in place' editing is possible, but that is unusual.

Do you have to use a text file? Would a random-access file be a better solution?

Richard.

Re: editing data with timer
Post by hammerjit on Jul 8th, 2015, 11:13pm

But the problem currently I'm facing is that due to the timer running I'm unable to change the value. If I turn off the timer I am able to change the value but the "total" field will not be correct.
Re: editing data with timer
Post by tsh73 on Jul 9th, 2015, 07:57am

Quote:
But the problem currently I'm facing is that due to the timer running I'm unable to change the value.

Sure. On timer, you read that value from file and overwrite what you typed.

Just move "read" part out of your loop.

I fiddled with it, without understanding what you actually doing. Have a look.
Code:
' nomainwin

    WindowWidth = 550
    WindowHeight = 410

    statictext #main.statictext1, "Record to Edit", 30, 86, 112, 20
    textbox #main.textbox2, 158, 81, 100, 25
    textbox #main.textbox3, 286, 81, 100, 25
    textbox #main.textbox4, 414, 81, 100, 25
    statictext #main.statictext5, "From (a)", 158, 51, 56, 20
    statictext #main.statictext7, "To (b)", 286, 51, 40, 20
    statictext #main.statictext8, "Total ( b-a x5)", 414, 51, 96, 20
    button #main.button9, "SAVE", [button9Click], UL, 158, 181, 42, 25
    open "untitled" for window as #main
    #main, "trapclose [quit1]"
    print #main, "font ms_sans_serif 0 16"

'----get from file here, once
OPEN "C:\test.txt" FOR INPUT AS #f
while eof(#f)=0
line input #f,l$
from$ = (word$(l$,1,",")):to$ = (word$(l$,2,",")):total$ = (word$(l$,3,","))
wend
close #f

    print #main.textbox2, from$
    print #main.textbox3, to$
    print #main.textbox4, total$
    print from$
    print to$
    print total$

'----------------------

timer 100, [main.inputLoop]
[main.inputLoop]   'wait here for input event

   PRINT #main.textbox2, "!contents? From$"
   PRINT #main.textbox3, "!contents? To$"
   'PRINT #main.textbox4, "!contents? Total$"
   'you should calculate Total instead
   Total = val(To$) - val(From$)*5
    Total$=str$( Total) 'so your Save routine work
   PRINT #main.textbox4, Total$

    wait

[button9Click]   'Perform action for the button named 'button9'

'OPEN "C:\test.txt" FOR OUTPUT AS #f
OPEN "C:\test.txt" FOR append AS #f 'if you need to add to file?
'while eof(#f)=0    'you output to file - no need to check for EOF
'line input #f,l$
print #f, From$;", ";To$; ", ";Total$
'wend
close #f

    wait

[quit1]
close #main

 

Re: editing data with timer
Post by hammerjit on Jul 9th, 2015, 09:01am

thank you so much tsh73...didn't realise can put the get file before main.inputloop...thanks again....