Author Topic: Best way to pass an open file handle from a process to a subroutine  (Read 4961 times)

Colin Alfke

  • Professional
  • ***
  • Posts: 23
We have a process that calls a standard (UniData) subroutine. The subroutine requires an open file handle be passed (it's a generic routine that won't know which file it's working on). Currently we're opening the file to a number in the process and passing it using
SAVEFILENAME = @FILES.OPENED<29>
OPEN 'FILENAME' TO 29
CALL MYSUB(@FILEVAR(29))
IF SAVEFILENAME # '' THEN
 OPEN SAVEFILENAME TO 29
END

Every once in a while the data from the screen that calls this process (using writes to "differentfilename") gets written to the file that's opened as 29. If I compile the code it looks like it everything is being tracked properly (as long as sb.filevar.s is working properly).

Following the subroutine call it also calls some selection processes that seem to complicate matters (adding files to the open.files list).

I've tried closing 29 when it comes back and savefilename was blank, but that actually causes write errors in a lot of cases....

Is there a better way to do this?

Thanks

precisonline

  • President
  • Administrator
  • Rock Star
  • *****
  • Posts: 1612
    • Precision Solutions
Re: Best way to pass an open file handle from a process to a subroutine
« Reply #1 on: June 23, 2011, 09:22:02 AM »
My standard recommendation is to avoid OPEN in paragraphs because it just causes all sorts of weirdness.  If you need to open a file and pass the file buffer as a parameter, I would recommend a subroutine using SB.OPEN.FILE to open the file and then pass the buffer returned from there.  This way your file can be in the SB+ file buffer cache rather than trying to work around it.
-Kevin
Accidents "happen"; success, however, is planned and executed.