Chapter 15: Utilities > fopen

fopen

Opens a file using the C runtime library used by the IMSL C Stat Library.

Synopsis

#include <imsls.h>

#include <stdio.h>

FILE *imsls_fopen (char *filename, char *mode)

Required Arguments

char *filename  (Input)
The name of the file to be opened.

char *mode  (Input)
The type of access to be permitted to the file. This string is passed to the C runtime function fopen, which determines the valid mode values.

Return Value

A pointer to the file structure, FILE, defined in stdio.h. To close the file, use imsls_fclose.

Description

The function imsls_fopen opens a file using the C runtime library used by the IMSL C Stat Library.  It is a wrapper around the standard C runtime function fopen.

Function imsls_fopen can always be used to open a file which will be used by the IMSL C Stat Library, but is required if an application has linked to multiple copies of the C runtime library, with each copy having its own set of file instructions. In this situation, using the C runtime function fopen can result in a file being opened with one copy of the C runtime library and reading or writing to it with a different copy, which may cause abnormal behavior or termination. Using imsls_fopen ensures that the same C runtime library is used for both the open operation and reading and writing within an IMSL C Stat Library function to which the file pointer has been passed as an input argument.

Note that imsls_fopen should only be used to open a file whose file pointer will be input to an IMSL C Stat Library function. Use imsls_fclose to close files opened with imsls_fopen.
This function is not prototyped in imsl.h. This is to avoid including stdio.h within imsl.h.

Example

This example writes a matrix to the file matrix.txt. The function imsls_fopen is used to open a file. This function returns a file pointer, which is passed to imsls_output_file. The matrix is written by imsls_f_write_matrix, which uses the file pointer from imsls_output_file. The function imsls_fclose is then used to close the file.

 

#include <imsls.h>

#include <stdio.h>

extern FILE* imsls_fopen(char* filename, char* mode);

extern void imsls_fclose(FILE* file);

 

int main()

{

    FILE  *file;

    float a[] = {

        1.1, 2.4, 3.6,

        4.3, 5.1, 6.7,

        7.2, 8.9, 9.3

    };

 

    file = imsls_fopen("matrix.txt", "w");

   

    imsls_output_file(IMSLS_SET_OUTPUT_FILE, file,

                      0);

    imsls_f_write_matrix("Matrix written matrix.txt",

                        3, 3, a, 0);

 

    imsls_fclose(file);

}

Output

The content below is stored in the matrix.txt file.

      Matrix written to matrix.txt
             1            2            3
1          1.1          2.4          3.6
2          4.3          5.1          6.7
3          7.2          8.9          9.3


RW_logo.jpg
Contact Support