Accessing Files in the MEM: Device

Updated December 2008

In some situations, it makes sense to load an entire file into your user memory partition and access it directly from memory. This can be accomplished using the MEM: device. Use the LOAD command to load the file into your user partition. Once loaded, you can open it and read/write to it just like any other random access file, simply by specifying MEM:<filename> in the OPEN statement.

If you write to the file while it is in your memory partition, you'll need to use SAVE.LIT or the equivalent MX_USRSAV function in order to write the updated file back to disk.

 

Note that LOAD.LIT dynamically allocates additional memory for any files so loaded, so it is not necessary to worry about the size of your pre-allocated memory partition.

Accessing a file using the MEM: device is naturally much faster than ordinary disk access (even when the file is cached). It is nearly as fast as memory map mode, and much faster than local copy or read-only modes. The biggest advantage that the MEM: approach has over the previous methods is that it can be used without making any program changes. The trick is to add OPTIONS=AUTO_MEMOPEN to miame.ini, which causes all BASIC file open operations to automatically check to see if the specified file is in memory before opening the disk copy. If it is already in memory, then it is accessed as if it had been using the MEM: device. This way, you can use MEM: access in specific programs merely by using LOAD.LIT to load the file prior to running the program.

Be careful when using AUTO_MEMOPEN that you don't inadvertently leave the file in memory longer than intended, because subsequent programs will act as if running normally, but any changes to the file will only affect the memory copy.

Update Note: Build 1114 of 27 May 2008

The MEM: device, as well as OPTIONS=AUTO_MEMOPEN, can now be used with sequential input files. For example:

OPEN #1, "MEM:MYFILE.TXT", INPUT

The above statement would work if myfile.txt was loaded into the user memory (using LOAD MYFILE.TXT).

OPEN #1, "DSK1:MYFILE.TXT[123,456], INPUT

As with RANDOM files, if OPTIONS=AUTO_MEMOPEN, then the above OPEN statement will first check user memory to see if myfile.txt is loaded, and if so, will use that copy directly.