Loading ...
Sorry, an error occurred while loading the content.

RE: [libertybasic] File handles using variables

Expand Messages
  • Stefan Pendl
    ... You should be able to use the MAPHANDLE command to create unique file handles using one common SUB. Below find an example to get some hints. ---code start
    Message 1 of 5 , Jun 5, 2008
    • 0 Attachment
      >
      > I want to beable to execute a sub that opens and reads a file, but I
      > want to beable to execute the sub (opening another file) from within
      > the sub. The only problem I have is that I cannot use the same file
      > handle to read the file. I could use several sub but I really do not
      > want to do that.
      >
      ---snip
      >
      > Now I know something like this has been done before with windows, but
      > I cannot seem to find the information (I've been searching for days)
      >

      You should be able to use the MAPHANDLE command to create unique file handles using one common SUB.
      Below find an example to get some hints.

      '---code start (watch for line wraps)
      global FileCount
      FileCount = 1

      call execscipt "myFile.txt", "#myFile"; FileCount

      sub execscipt scriptfile$, Handle$
      open scriptfile$ for input as #fhandle
      maphandle #fhandle, Handle$
      FileCount = FileCount + 1

      while eof(#Handle$) <> 0
      line input #Handle$, txt$
      wend

      call execscipt "myFile2.txt", "#myFile"; FileCount

      close #Handle$
      end sub
      '---code end

      ---
      Stefan Pendl
    • adelphospro2000
      ... file handles using one common SUB. ... Thanks! But the seek command doesn t work now (it says bad file handle ): seek #currenthandle$, avar Is this a
      Message 2 of 5 , Jun 5, 2008
      • 0 Attachment
        --- In libertybasic@yahoogroups.com, "Stefan Pendl"
        <pendl2megabit@...> wrote:
        >
        > >
        > > I want to beable to execute a sub that opens and reads a file, but I
        > > want to beable to execute the sub (opening another file) from within
        > > the sub. The only problem I have is that I cannot use the same file
        > > handle to read the file. I could use several sub but I really do not
        > > want to do that.
        > >
        > ---snip
        > >
        > > Now I know something like this has been done before with windows, but
        > > I cannot seem to find the information (I've been searching for days)
        > >
        >
        > You should be able to use the MAPHANDLE command to create unique
        file handles using one common SUB.
        > Below find an example to get some hints.
        >
        > '---code start (watch for line wraps)
        > global FileCount
        > FileCount = 1
        >
        > call execscipt "myFile.txt", "#myFile"; FileCount
        >
        > sub execscipt scriptfile$, Handle$
        > open scriptfile$ for input as #fhandle
        > maphandle #fhandle, Handle$
        > FileCount = FileCount + 1
        >
        > while eof(#Handle$) <> 0
        > line input #Handle$, txt$
        > wend
        >
        > call execscipt "myFile2.txt", "#myFile"; FileCount
        >
        > close #Handle$
        > end sub
        > '---code end
        >
        > ---
        > Stefan Pendl
        >

        Thanks! But the seek command doesn't work now (it says 'bad file
        handle'):
        seek #currenthandle$, avar

        Is this a bug?
      • Stefan Pendl
        ... Are you opening the file for binary? Handle variables are not supported by all file handling functions, mostly only by those which handle sequential files.
        Message 3 of 5 , Jun 5, 2008
        • 0 Attachment
          >
          > Thanks! But the seek command doesn't work now (it says 'bad file
          > handle'):
          > seek #currenthandle$, avar
          >
          > Is this a bug?
          >

          Are you opening the file for binary?

          Handle variables are not supported by all file handling functions, mostly only by those which handle sequential files.

          ---
          Stefan Pendl
        • adelphospro2000
          ... mostly only by those which handle sequential files. ... Thanks for all your help! No I did not open the file for binary. Looking at the help file I thought
          Message 4 of 5 , Jun 5, 2008
          • 0 Attachment
            --- In libertybasic@yahoogroups.com, "Stefan Pendl"
            <pendl2megabit@...> wrote:
            >
            > >
            > > Thanks! But the seek command doesn't work now (it says 'bad file
            > > handle'):
            > > seek #currenthandle$, avar
            > >
            > > Is this a bug?
            > >
            >
            > Are you opening the file for binary?
            >
            > Handle variables are not supported by all file handling functions,
            mostly only by those which handle sequential files.
            >
            > ---
            > Stefan Pendl
            >

            Thanks for all your help!

            No I did not open the file for binary. Looking at the help file I
            thought seek would not work with files opened for input but it did. It
            doesn't work with handle variables. No problem though, I worked around
            it. Unfortunately this is what I had to do:
            In the script file I'm processing loops. Using the seek command I
            simply went back to the start of the loop and executed the section
            again, but of course seek doesn't work now. So what I'm doing now is
            writing the whole loop section to a new file and executing it again
            and again until the loop is over. It's a little slower but it gets the
            job done! Besides, most looping will only be done when maps are being
            loaded.
          Your message has been successfully submitted and would be delivered to recipients shortly.