CNLMath : Utilities : fopen
fopen
Opens a file using the C runtime library used by the IMSL C Math Library.
Synopsis
#include <imsl.h>
#include <stdio.h>
FILE *imsl_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 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.
Description
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.
Note: This function is not prototyped in imsl.h. This is to avoid including stdio.h within imsl.h. An extern declaration should be explicitly used to assure compatibility with linkers.
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 int imsl_fclose(FILE* file);
 
int main()
{
FILE *ofile;
float x[] = {3.0, 2.0, 1.0};
 
imsl_f_write_matrix ("x (default file)", 1, 3, x, 0);
 
ofile = imsl_fopen("myfile", "w");
imsl_output_file(
IMSL_SET_OUTPUT_FILE, ofile,
0);
imsl_f_write_matrix ("x (myfile)", 1, 3, x,
0);
 
imsl_fclose(ofile);
}
Output
 
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