Author |
Topic: Random Access Procedures (Read 106 times) |
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Random Access Procedures
« Reply #17 on: Oct 23rd, 2015, 7:19pm » |
|
At the risk of antagonizing Richard even further ... :D ... I do like to find the edges of proper operation.
LB reports a syntax error in the open line and LBB just says "Mistake!"
EDIT: Adding the "result =" bit-bucket for the FUNCTION makes the compilers happy.
Code:
path$ = ""
fileName$ = "myfile.txt"
fileHandle$ = "#file1"
recordLen = 10
field1$ = "1234"
field2$ = "1234"
field3$ = "1234"
field4$ = "1234"
' EDIT: openRAF(path$,fileName$,fileHandle$,recordLen)
result = openRAF(path$,fileName$,fileHandle$,recordLen)
put #fileHandle$, 1
call closeRAF fileHandle$
field1$ = ""
field2$ = ""
field3$ = ""
field4$ = ""
'EDIT: openRAF(path$,fileName$,fileHandle$,recordLen)
result = openRAF(path$,fileName$,fileHandle$,recordLen)
get #fileHandle$, 1
call closeRAF fileHandle$
print "1: "; field1$
print "2: "; field2$
print "3: "; field3$
print "4: "; field4$
end
sub closeRAF fileHandle$
close #fileHandle$
end sub
function fileExists(path$, filename$)
' dimension the array info$( at beginning of program
' dim info$(10, 10)
' e.g. if fileExists(DefaultDir$, "users.dat") then
files path$, filename$, info$()
fileExists = val(info$(0, 0)) 'non zero is true
end function
function openRAF(path$, filename$, fileHandle$, recordLen)
' file must exist?
' fileExists(path$, filename$)
' fileHandle$ = "#yourFileHandle"
open filename$ for random as #1 len = recordLen ' open and define record
maphandle #1, fileHandle$
openRAF = setRAFfields(fileHandle$, recordLen)
end function
function setRAFfields(fileHandle$, recordLen)
'field lengths and field names have to be modified per program
'call after openRAF
'set the fields in RAF
if (1+2+3+4) = recordLen then ' something to make one think about field lengths
field #fileHandle$,_
1 as field1$,_
2 as field2$,_
3 as field3$,_
4 as field4$
setRAFfields = 0
else
setRAFfields = 1 ' error condition
end if
end function
|
| « Last Edit: Oct 24th, 2015, 10:06am by joker » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Random Access Procedures
« Reply #18 on: Oct 23rd, 2015, 9:32pm » |
|
on Oct 23rd, 2015, 7:19pm, pnlawrence wrote:| LB reports a syntax error in the open line and LBB just says "Mistake!" |
|
It may be that you have previously used another language which allows a function to be called without assigning the returned value to anything, but Liberty BASIC doesn't. If you want to call a function, but ignore the value it returns, you must assign it to something, for example:
Code: ignoreThis = openRAF(path$,fileName$,fileHandle$,recordLen) Richard.
|
|
Logged
|
|
|
|
joker
Global Moderator
member is offline


Gender: 
Posts: 157
|
 |
Re: Random Access Procedures
« Reply #19 on: Oct 24th, 2015, 09:51am » |
|
Now, there's a difference between SUB and FUNCTION that I missed. Thanks!
|
| « Last Edit: Oct 24th, 2015, 09:51am by joker » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline


Posts: 1348
|
 |
Re: Random Access Procedures
« Reply #20 on: Oct 24th, 2015, 10:40am » |
|
on Oct 23rd, 2015, 9:32pm, Richard Russell wrote:| If you want to call a function, but ignore the value it returns, you must assign it to something |
|
I occasionally use this construction, to avoid creating a dummy variable, but it's a bit ugly (and relies on a 'feature' of LB/LBB that rem and ' are not always equivalent):
Code: if openRAF(path$,fileName$,fileHandle$,recordLen) then rem Richard.
|
|
Logged
|
|
|
|
|