### Direct I/O to Memory Files (C)

Use memory files to reduce disk I/O during program execution. Memory files differ from the other file types only in that they are temporary files that reside in memory. You can write to and read from a memory file just as you do with a disk file.

You must specify the /Sv compiler option to use memory files.

You can create a memory file in two ways:

- Specify type=memory directly in your source code. For example, the following statement opens a memory file:

         stream = fopen("memfile.txt", "w, type=memory");

- Use the data definition name (ddname) in the fopen call and the set command to specify the file you want your application to open. For example, the following statement opens a file whose name is stored in the ddname MEMFILE.

         stream=fopen("DD:MEMFILE", "w");

  Before you run your application, use the set command. For example, the following command assigns the filename memfile.txt to the ddname MEMFILE.

         SET DD:MEMFILE=memfile.txt, memory(y)

> You must specify the /Sh compiler option to use ddnames.

Once a memory file has been created, it can be accessed by the module that created it as well as by any other function within the same process. The memory file remains accessible until the file is removed by the remove function or until the program has terminated.

You can request that the temporary files created by the tmpfile function be either disk files or memory files. By default, tmpfile creates disk files. To have temporary files created as memory files, set the TEMPMEM environment variable to ON:

       SET TEMPMEM=on

The word "on" can be in any case. You must still specify the memoryfiles Language Option.

#### Memory File Restrictions and Considerations

- Using memory files usually increases program execution speed. However, if your program is running in an environment where the operating system is paging, speed may be lost if your memory files are large.
- Memory files do not undergo any conversion of the new-line character. Data is not altered on input or output.
- Memory files are unbuffered, and the blksize attribute is ignored. No validation is performed for the path or file name used.
- Memory file names are case sensitive. For example, the file a.a is not the same memory file as A.A:

         fopen("A.A","w,type=memory");
         remove("a.a");

  The above call to remove will not remove memory file A.A because the file name is in uppercase. Because memory files are always checked first, the function will look for memory file a.a, and if that file does not exist, it will remove the disk file a.a (or A.A, because disk files are not case sensitive).

- Memory files are private to the process that created them, and they cannot be shared with any other process, including child processes. Also, memory files cannot be shared through the system function.
- Redirection to memory files from the command line is not supported.
- When using fopen to open a memory file in write or append mode, you must ensure that the file is not already open.
- A call to fopen that tries to open a file with the same name as an existing memory file accesses the memory file, even if you do not specify type=memory in the fopen call.

Stream Processing

Redirect Standard Streams
Open Streams Using Data Definition Names