User Tools

Site Tools


reading_20and_20writing_20arrays_20in_20files

Differences

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

Link to this comparison view

reading_20and_20writing_20arrays_20in_20files [2018/03/31 13:19]
127.0.0.1 external edit
reading_20and_20writing_20arrays_20in_20files [2018/04/17 18:24] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Jon Ripley, May 2011//\\ \\ //​Alternative method added by Steve Drain, Oct 2013//\\ \\  BBC BASIC for Windows does not provide a simple method of writing arrays to a file and reading them back again, doing so traditionally requires looping through the array and saving each separate item individually.\\ \\  The PROC_ReadArray and PROC_WriteArray routines below provide equivalents to PRINT# and INPUT# for arrays and are faster than the traditional method in most cases.\\ \\  //by Jon Ripley, May 2011//\\ \\ //​Alternative method added by Steve Drain, Oct 2013//\\ \\  BBC BASIC for Windows does not provide a simple method of writing arrays to a file and reading them back again, doing so traditionally requires looping through the array and saving each separate item individually.\\ \\  The PROC_ReadArray and PROC_WriteArray routines below provide equivalents to PRINT# and INPUT# for arrays and are faster than the traditional method in most cases.\\ \\ 
 +<code bb4w>
         PROC_WriteArray(fileHandle%,​ ^array())         PROC_WriteArray(fileHandle%,​ ^array())
         PROC_ReadArray(fileHandle%,​ ^array())         PROC_ReadArray(fileHandle%,​ ^array())
 +</​code>​
 \\  When reading and writing arrays you must prefix the array variable name with the address of operator, the **^** symbol, this allows us to pass all types of array to a single routine rather than requiring a separate routine for each.\\ \\ **PROC_WriteArray** writes the contents of an array to the file specified by fileHandle%.\\ \\  \\  When reading and writing arrays you must prefix the array variable name with the address of operator, the **^** symbol, this allows us to pass all types of array to a single routine rather than requiring a separate routine for each.\\ \\ **PROC_WriteArray** writes the contents of an array to the file specified by fileHandle%.\\ \\ 
 +<code bb4w>
         DIM array(9, 9, 9, 9)         DIM array(9, 9, 9, 9)
         REM Initialise array         REM Initialise array
Line 10: Line 13:
         PROC_WriteArray(fileHandle%,​ ^array())         PROC_WriteArray(fileHandle%,​ ^array())
         CLOSE#​fileHandle%         CLOSE#​fileHandle%
 +</​code>​
 \\ **PROC_ReadArray** reads data from the file specified by fileHandle% and stores it in the specified array.\\ \\  \\ **PROC_ReadArray** reads data from the file specified by fileHandle% and stores it in the specified array.\\ \\ 
 +<code bb4w>
         DIM array(1000, 10)         DIM array(1000, 10)
         fileHandle% = OPENIN("​data.dat"​)         fileHandle% = OPENIN("​data.dat"​)
         PROC_ReadArray(fileHandle%,​ ^array())         PROC_ReadArray(fileHandle%,​ ^array())
         CLOSE#​fileHandle%         CLOSE#​fileHandle%
 +</​code>​
 \\  For more information about **PROC_SwapMemory** see [[/​swapping%20the%20contents%20of%20two%20areas%20of%20memory|swapping the contents of two areas of memory]].\\ \\  The full code for PROC_ReadArray,​ PROC_WriteArray and PROC_SwapMemory is as follows:\\ \\  \\  For more information about **PROC_SwapMemory** see [[/​swapping%20the%20contents%20of%20two%20areas%20of%20memory|swapping the contents of two areas of memory]].\\ \\  The full code for PROC_ReadArray,​ PROC_WriteArray and PROC_SwapMemory is as follows:\\ \\ 
 +<code bb4w>
         DEF PROC_WriteArray(file%,​ parr%):​LOCAL write%:​write%=TRUE         DEF PROC_WriteArray(file%,​ parr%):​LOCAL write%:​write%=TRUE
         DEF PROC_ReadArray(file%,​ parr%):​LOCAL write%         DEF PROC_ReadArray(file%,​ parr%):​LOCAL write%
Line 78: Line 85:
         ENDPROC         ENDPROC
        
 +</​code>​
 \\  Thanks to Richard Russell for the SwapMemory function.\\ \\  Thanks to Richard Russell for the SwapMemory function.\\
 ---- ----
Line 86: Line 94:
   * a string target array must be empty   * a string target array must be empty
 \\  \\ 
 +<code bb4w>
         DEF PROC_WriteArray(file%,​pntr%)         DEF PROC_WriteArray(file%,​pntr%)
         LOCAL type%,​size%,​numb%,​temp%         LOCAL type%,​size%,​numb%,​temp%
Line 154: Line 163:
         pntr%=temp%         pntr%=temp%
         ENDPROC         ENDPROC
 +</​code>​
reading_20and_20writing_20arrays_20in_20files.txt ยท Last modified: 2018/04/17 18:24 by tbest3112