Temporary Files

A particularly good use of the MEM: device is for temporary files. Such files are typically only used by a single program anyway, so there is no point in having them on disk, except that we might not previously been able to afford the memory. Now that memory is cheap, and dynamically allocated as needed by the MEMORY and LOAD commands, there is little reason not to put any temporary files that are heavily accessed directly in memory.

Unfortunately, there is currently no way to create a file directly in memory. The ALLOCATE statement does not support the MEM: statement. So you would have to first allocate the file on disk then load it in memory, as in this example:

! Example of creating, loading, using a temp file in memory

! First, allocate the file on disk…  (we use the device TMP0:

! which, in the case of Windows, should point to a local

! workstation drive rather than the server

allocate "TMP0:MYTEMP.DAT", BLOCKS

 

! Next, load it into our user memory

xcall MIAMEX,108, IDX, "TMP0:MYTEMP.DAT"

 

! Remove it from disk (no longer needed there)

kill "TMP0:MYTEMP.DAT"

 

! Now open the file in memory…

open #1, "MEM:MYTEMP.DAT", RANDOM, RECSIZ, RECVAR

 

! Read and write to the file just like any other file

read #1, REC

write #1, REC

<etc>

 

! Close file and delete it from memory

close #1

xcall MIAMEX, MX_USRDEL, IDX, "MYTEMP.DAT",