Liberty BASIC Community Forum
« File handle variables?? »

Welcome Guest. Please Login or Register.
May 29th, 2017, 08:20am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: File handle variables??  (Read 200 times)
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5292
xx Re: File handle variables??
« Reply #1 on: May 18th, 2017, 01:24am »

No, open statements require literal values. The handle variable can then be used to address controls in a loop.

It would be a great addition to 4.5 if we could use variables to create controls and open files.
User IP Logged

CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2792
xx Re: File handle variables??
« Reply #2 on: May 18th, 2017, 07:22am »

on May 18th, 2017, 01:24am, Rod wrote:
No, open statements require literal values. The handle variable can then be used to address controls in a loop.

It would be a great addition to 4.5 if we could use variables to create controls and open files.

We are past the point of adding things. Sorry. I am about to release v4.5.1.

-Carl
User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
wscbill
New Member
Image


member is offline

Avatar




PM


Posts: 2
xx Re: File handle variables??
« Reply #3 on: May 18th, 2017, 1:15pm »

Perhaps it is not supposed to, but file handle variables do work with open statements in certain circumstances. Changing the original example as follows works, the files are created and populated, and no errors are raised.

The location of the close statement seems to have an impact. If the close is moved outside the loop, errors are flagged.

Code:

    for x = 1 to 3
        var$="#"+str$(x)
        open "newFile";x;".csv" for output as #var$

        print #var$, "file";x;",a,b,c,d,e"

        close #var$
    next x

    end


 
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5292
xx Re: File handle variables??
« Reply #4 on: May 18th, 2017, 3:28pm »

I suspect you might be getting one file opened and closed not three. Not had time to test but literals are definitely required for file and control creation.
User IP Logged

wscbill
New Member
Image


member is offline

Avatar




PM


Posts: 2
xx Re: File handle variables??
« Reply #5 on: May 19th, 2017, 07:15am »

Also works reading files recursively. This example opens and reads a file consisting of comments and file names. Each line read is printed. When a file name is read the code opens and reads that file.

Code:
global lvl
call readFile "file1.txt"
end

' read files recursively
sub readFile parm$

    ' level of recursion
    lvl = lvl + 1

    ' open the file
    print space$(5*lvl);"Open ";parm$
    open DefaultDir$+"\"+parm$ for input as #handle$

    ' read each record
    do

        line input #handle$, L$ 
        select case

            ' print comments
            case left$(L$,1) = "'"
                print space$(5*lvl);L$

            ' read the specified file
            case  else
                print space$(5*lvl);L$
                call readFile word$(L$,1)

        end select

    ' continue reading the records in file
    loop until eof(#handle$) <> 0

    ' close the file at eof
    close #handle$

    ' back up one level
    lvl = lvl - 1
end sub

 


generates this output
Code:
     Open file1.txt
     'line 1 of file 1
     'line 2 of file 1
     file2.txt - line 3 of file 1
          Open file2.txt
          'line 1 of file2
          'line 2 of file2
          'line 3 of file2
          file3.txt - line 4 of file2
               Open file3.txt
               file4.txt - line 1 of file3
                    Open file4.txt
                    'line 1 of file 4
                    'line 2 of file 4
                    'line 3 of file 4
                    'line 4 of file 4
                    file5.txt - 'line 5 of file 4
                         Open file5.txt
                         'line 1 of file 5
                         'line 2 of file 5
                         'line 3 of file 5
                         'line 4 of file 5
                         'line 5 of file 5
               'line 2 of file3
               'line 3 of file3
               'line 4 of file3
               'line 5 of file3
          'line 5 of file2
     'line 4 of file 1
     'line 5 of file 1


 


using these input files.

file1.txt
'line 1 of file 1
'line 2 of file 1
file2.txt - line 3 of file 1
'line 4 of file 1
'line 5 of file 1

file2.txt
'line 1 of file2
'line 2 of file2
'line 3 of file2
file3.txt - line 4 of file2
'line 5 of file2

file3.txt
file4.txt - line 1 of file3
'line 2 of file3
'line 3 of file3
'line 4 of file3
'line 5 of file3

file4.txt
'line 1 of file 4
'line 2 of file 4
'line 3 of file 4
'line 4 of file 4
file5.txt - 'line 5 of file 4

file5.txt
'line 1 of file 5
'line 2 of file 5
'line 3 of file 5
'line 4 of file 5
'line 5 of file 5
« Last Edit: May 19th, 2017, 09:56am by wscbill » User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5292
xx Re: File handle variables??
« Reply #6 on: May 19th, 2017, 1:19pm »

I have not looked at your recursive code. Looking at the original code you posted step through in the debugger. You should notice that the var$ variable holds what you assign it originally but, as soon as the open statement is processed the contents of var$ changes. So it seems you can't use a handle variable to assign a name to a control, the open statement assigns a value to the literal name which you must use thereafter.

To be clear, you are meant to use a literal value to name a control's handle like #file1 That name will point to the file. You are not supposed to use a #handle variable to open resources.

You may use the literal name to create a #handle variable after the control is created eg v$="#file1" and then address the file with #v$

Swimming with the tide is always easier.
« Last Edit: May 19th, 2017, 1:31pm by Rod » User IP Logged

psycho
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 26
xx Re: File handle variables??
« Reply #7 on: May 24th, 2017, 8:46pm »

Thanks for all the replies.
I was given an alternate method that worked for me.
User IP Logged



John "Psycho" Siejkowski
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls