User Tools

Site Tools


exceeding_20the_20limit_20of_208_20open_20files

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

exceeding_20the_20limit_20of_208_20open_20files [2018/03/31 13:19]
127.0.0.1 external edit
exceeding_20the_20limit_20of_208_20open_20files [2018/04/17 16:05] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, October 2015//\\ \\  Normally only a maximum of eight files may be open simultaneously in //BBC BASIC for Windows//; an attempt to open a ninth file will result in the **Too many open files** error (ERR = 192). It is possible to work around this limitation by opening-and-closing the file for each transaction (so that only one file is open at a time), for example to write a record to a file one might do:\\  //by Richard Russell, October 2015//\\ \\  Normally only a maximum of eight files may be open simultaneously in //BBC BASIC for Windows//; an attempt to open a ninth file will result in the **Too many open files** error (ERR = 192). It is possible to work around this limitation by opening-and-closing the file for each transaction (so that only one file is open at a time), for example to write a record to a file one might do:\\ 
 +<code bb4w>
         file% = OPENUP(file$)         file% = OPENUP(file$)
         PTR#file% = EXT#file%         PTR#file% = EXT#file%
         PRINT #file$, record$         PRINT #file$, record$
         CLOSE #file%         CLOSE #file%
 +</​code>​
 However this approach has a number of shortcomings:​ it's messy, it's (relatively) slow - especially if the file is on a network share - and since the file is constantly being opened and closed another process could in principle modify it whilst it is being written.\\ \\  Fortunately there is an alternative approach which allows an arbitrarily large number of files to be open simultaneously. It involves using replacements for the usual OPENIN, OPENOUT and OPENUP functions. Here is a demonstration program which illustrates how it is possible to have (in this case) 12 files open at the same time:​\\ ​ However this approach has a number of shortcomings:​ it's messy, it's (relatively) slow - especially if the file is on a network share - and since the file is constantly being opened and closed another process could in principle modify it whilst it is being written.\\ \\  Fortunately there is an alternative approach which allows an arbitrarily large number of files to be open simultaneously. It involves using replacements for the usual OPENIN, OPENOUT and OPENUP functions. Here is a demonstration program which illustrates how it is possible to have (in this case) 12 files open at the same time:​\\ ​
 +<code bb4w>
         NoOfFiles% = 12         NoOfFiles% = 12
         DIM Chan%(NoOfFiles%)         DIM Chan%(NoOfFiles%)
Line 63: Line 66:
  
         END         END
 +</​code>​
 Note that if this technique is used the CLOSE #0 statement (which normally closes all open files) will close only the first eight.\\ \\  Here are the replacement functions which allow this magic to happen:​\\ ​ Note that if this technique is used the CLOSE #0 statement (which normally closes all open files) will close only the first eight.\\ \\  Here are the replacement functions which allow this magic to happen:​\\ ​
 +<code bb4w>
         DEF FNopenin(F$)         DEF FNopenin(F$)
         LOCAL E%,F%,H%         LOCAL E%,F%,H%
Line 84: Line 89:
         IF H% SWAP H%,!500 : SWAP E%,!532 : IF H% F% = H%         IF H% SWAP H%,!500 : SWAP E%,!532 : IF H% F% = H%
         = F%         = F%
 +</​code>​
 Please note that only the first eight files will be buffered by BASIC, so files in excess of that number may be accessed a little more slowly.\\ \\  Please also be aware that the value returned by these replacement functions, for files in excess of the initial eight, is the native Windows file handle. For this to work the Windows file handle must exceed 16, otherwise BB4W will mistake it for a BASIC file channel number. This is normally reliable but there is **nothing in the Microsoft API documentation which states that a file handle will always be greater than 16**. Please note that only the first eight files will be buffered by BASIC, so files in excess of that number may be accessed a little more slowly.\\ \\  Please also be aware that the value returned by these replacement functions, for files in excess of the initial eight, is the native Windows file handle. For this to work the Windows file handle must exceed 16, otherwise BB4W will mistake it for a BASIC file channel number. This is normally reliable but there is **nothing in the Microsoft API documentation which states that a file handle will always be greater than 16**.
exceeding_20the_20limit_20of_208_20open_20files.txt ยท Last modified: 2018/04/17 16:05 by tbest3112