Next Previous Contents

99. SLang_pop_fileptr

Synopsis

Pop a file pointer

Usage

int SLang_pop_fileptr (SLang_MMT_Type **mmt, FILE **fp)

Description

SLang_pop_fileptr pops a file pointer from the S-lang run-time stack. It returns zero upon success, or -1 upon failure.

A S-lang file pointer (SLANG_FILEPTR_TYPE) is actually a memory managed object. For this reason, SLang_pop_fileptr also returns the memory managed object via the argument list. It is up to the calling routine to call SLang_free_mmt to free the object.

Example

The following example illustrates an application defined intrinsic function that writes a user defined double precision number to a file. Note the use of SLang_free_mmt:

     int write_double (void)
     {
        double t;
        SLang_MMT_Type *mmt;
        FILE *fp;
        int status;

        if (-1 == SLang_pop_double (&d, NULL, NULL))
          return -1;
        if (-1 == SLang_pop_fileptr (&mmt, &fp))
          return -1;
        
        status = fwrite (&d, sizeof (double), 1, fp);
        SLang_free_mmt (mmt);
        return status;
     }
This function can be used by a S-lang function as follows:
     define write_some_values ()
     {
        variable fp, d;

        fp = fopen ("myfile.dat", "wb");
        if (fp == NULL)
          error ("file failed to open");
        for (d = 0; d < 10.0; d += 0.1)
          {
             if (-1 == write_double (fp, d))
               error ("write failed");
          }
        if (-1 == fclose (fp))
          error ("fclose failed");
     }
See Also

SLang_free_mmt, SLang_pop_double


Next Previous Contents