fopen

Opens a file using the C runtime library used by the PyIMSL Math Library.

Synopsis

fopen (filename, 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 structure. To close the file, use fclose.

Description

The function fopen opens a file for reading and writing. It is a wrapper around the standard C runtime function fopen.

Note that fopen should only be used to open a file whose file pointer will be input to an PyIMSL function. Use fclose to close files opened with fopen.

Example

This example writes a matrix to the file matrix.txt. The function fopen is used to open a file. This function returns a file pointer, which is passed to outputFile. The matrix is written by writeMatrix, which uses the file pointer from outputFile. The function fclose is then used to close the file.


from pyimsl.math.fopen import fopen
from pyimsl.math.fclose import fclose
from pyimsl.math.writeMatrix import writeMatrix
from pyimsl.math.outputFile import outputFile
a = [1.1, 2.4, 3.6,
     4.3, 5.1, 6.7,
     7.2, 8.9, 9.3]
file = fopen("matrix.txt", "w")
ofile = []
outputFile(setOutputFile=file)
writeMatrix("Matrix written matrix.txt", a)
fclose(file)

with open("matrix.txt", "r") as output:
    print(output.read())

Output

 
                          Matrix written matrix.txt
          1            2            3            4            5            6
        1.1          2.4          3.6          4.3          5.1          6.7
 
          7            8            9
        7.2          8.9          9.3