Example program

The following examples show the use of a memory file. Program CCNGMF3 (Figure 1) creates a memory file, calls program CCNGMF4 (Figure 2), and redirects the output of the called program to the memory file. When control returns to the first program, the program reads and prints the string in the memory file.

For more information on the system() library function, see z/OS XL C/C++ Runtime Library Reference.

Figure 1. Memory file example, part 1
/* this example demonstrates the use of a memory file */
/* part 1 of 2-other file is CCNGMF4 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
    FILE *fp;
    char buffer[20];
    char *rc;
/* Open the memory file to create it                                */
    if ((fp = fopen("PROG.DAT","wb+,type=memory")) != NULL)
     {
       /* Close the memory file so that it can be used as stdout */
       fclose(fp);

       /* Call CCNGMF4 and redirect its output to memory file   */
       /* CCNGMF4 must be an executable MODULE                  */
       system("CCNGMF4 >PROG.DAT");
/* Now print the string contained in the file                       */

       fp = fopen("PROG.DAT","rb");
       rc = fgets(buffer,sizeof(buffer),fp);
       if (rc == NULL)
       {
          perror(" Error reading from file ");
          exit(99);
       }
       printf("%s", buffer);
     }

     return(0);
}

Figure 2 redirects the output of the called program to the memory file.

Figure 2. Memory file example, part 2
/* this example demonstrates the use of a memory file */
/* part 2 of 2-other file is CCNGMF3 */

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
       char item1[] = "Hello World\n";
       int rc;
/* Write the data to the stdout which, at this point, has been
   redirected to the memory file                                    */
       rc = fputs(item1,stdout);
       if (rc == EOF) {
          perror("Error putting to file ");
          exit(99);
       }

       return(0);

}