LB Booster
« Random Access Procedures »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 05:00am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: Random Access Procedures  (Read 103 times)
joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: Random Access Procedures
« Reply #9 on: Oct 22nd, 2015, 2:40pm »

So, am I barking up the wrong tree and going to find out after I build a lot of stuff? I'd like to know the chances of success now! ;D

Code:
function(OpenRAF path$, filename$, fileHandle$, recordLen)
    ' file must exist?
    open filename$ for random as #fileHandle$ len = recordLen ' open and define record
end function
 
sub CloseRAF fileHandle$
    close #fileHandle$
end sub

function fileExists(path$, filename$)
    'dimension the array info$( at the beginning of your program
    'dim info$(10, 10)
    'if fileExists(DefaultDir$, "users.dat") then
    files path$, filename$, info$()
    fileExists = val(info$(0, 0)) 'non zero is true
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
    endif
end function     
 
« Last Edit: Oct 22nd, 2015, 2:41pm by joker » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Random Access Procedures
« Reply #10 on: Oct 22nd, 2015, 10:10pm »

on Oct 22nd, 2015, 2:40pm, pnlawrence wrote:
Code:
    open filename$ for random as #fileHandle$ len = recordLen 

That doesn't work (even in LBB!), you must use MAPHANDLE:

Code:
    open filename$ for random as #1 len = recordLen
    maphandle #1, fileHandle$ 

Richard.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: Random Access Procedures
« Reply #11 on: Oct 23rd, 2015, 10:45am »

Funny how the brain works! "#fileHandle$" has shown up in some of my searches, but not in that form.

I should have used/assumed the form fileHandle$ = "#" + {path} + fileName$.

Being generic is getting more difficult. I'm starting to wonder if its worth the time for my project.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: Random Access Procedures
« Reply #12 on: Oct 23rd, 2015, 11:04am »

Curses! That won't work either. Missing "#" in OPEN.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Random Access Procedures
« Reply #13 on: Oct 23rd, 2015, 11:21am »

on Oct 23rd, 2015, 11:04am, pnlawrence wrote:
Curses! That won't work either. Missing "#" in OPEN.

The code I listed does work - I tested it before posting as I always try to do. Here's the entire earlier demo from the 'Numeric field length RAF' thread modified to use a handle variable to prove that it can be done (in LBB):

Code:
    handle$ = "#MyFileHandle" ' the handle variable

    field1 = 1/3
    field2 = 1/3

    open "test.raf" for random as #1 len = 20
    maphandle #1, handle$
    field #handle$, 6 as field1, 14 as field2
    put #handle$, 1
    close #handle$

    open "test.raf" for random as #2 len = 20
    maphandle #2, handle$
    field #handle$, 6 as field1$, 14 as field2$
    get #handle$, 1
    close #handle$

    print chr$(34);field1$;chr$(34)
    print chr$(34);field2$;chr$(34) 

This doesn't work in LB 4.04 or 4.5.0 so without LBB you really would be screwed.

Richard.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: Random Access Procedures
« Reply #14 on: Oct 23rd, 2015, 11:29am »

Thanks, Richard. I am studying every word you write.

This "library" seems to pass the RUN test (no program), but with an "incompatibility" error message.

EDIT: Well, of course it doesn't "RUN", because it has an "ERROR". The error message implies that it was somehow corrected, but there's no output.

Code:
function OpenRAF(path$, filename$, fileHandle$, recordLen)
    ' file must exist?
    open filename$ for random as #fileHandle$ len = recordLen ' open and define record
end function
 
sub CloseRAF fileHandle$
    close #fileHandle$
end sub

function fileExists(path$, filename$)
    'dimension the array info$( at the beginning of your program
    'dim info$(10, 10)
    'if fileExists(DefaultDir$, "users.dat") then
    files path$, filename$, info$()
    fileExists = val(info$(0, 0)) 'non zero is true
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
    endif
end function                    
 
« Last Edit: Oct 23rd, 2015, 12:30pm by joker » User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx Re: Random Access Procedures
« Reply #15 on: Oct 23rd, 2015, 11:32am »

Let me paraphrase your comment ...

Quote:
... without LBB you really would be screwed.


Without LBB you really are screwed!
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Random Access Procedures
« Reply #16 on: Oct 23rd, 2015, 1:07pm »

on Oct 23rd, 2015, 11:29am, pnlawrence wrote:
I am studying every word you write.

Apparently not acting upon however! :-)

Quote:
This "library" seems to pass the RUN test (no program), but with an "incompatibility" error message.

It still includes the line that doesn't work (either in LB or LBB):

Code:
    open filename$ for random as #fileHandle$ len = recordLen ' open and define record 

I have twice listed the correct code, but for some reason you have failed to incorporate it.

You also have an endif where you meant end if.

Richard.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx 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 » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.
User IP Logged

joker
Global Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 157
xx 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 » User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx 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.

User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »


This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls