Opens a file using the C runtime library used by the IMSL C Math Library.
#include <imsl.h>
#include <stdio.h>
FILE *imsl_fopen (char *filename, char *mode)
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.
A pointer for the file structure, FILE, defined in stdio.h. To close the file, use imsl_fclose. If there is a fatal error, then NULL is returned.
The function imsl_fopen opens a file using the C runtime library used by the IMSL C Math Library. It is a wrapper around the standard C runtime function fopen.
Function imsl_fopen can always be used to open a file which will be used by the IMSL C Math 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 imsl_fopen ensures that the same C runtime library is used for both the open operation and reading and writing within an IMSL C Math Library function to which the file pointer has been passed as an input argument.
Note that imsl_fopen
should only be used to open a file whose file pointer will be input to an IMSL C
Math Library function. Use imsl_fclose
to close files opened with imsl_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 imsl_fopen is used to open a file. This function returns a file pointer, which is passed to imsl_output_file. The matrix is written by imsl_f_write_matrix, which uses the file pointer from imsl_output_file. The function imsl_fclose is then used to close the file.
#include <imsl.h>
#include <stdio.h>
extern FILE* imsl_fopen(char* filename, char* mode);
extern void imsl_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 = imsl_fopen("matrix.txt", "w");
imsl_output_file(IMSL_SET_OUTPUT_FILE, file, 0);
imsl_f_write_matrix("Matrix written matrix.txt",
3, 3, a, 0);
imsl_fclose(file);
}
The content below is stored in the matrix.txt file.
Matrix written to file 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