fopen

Opens a file for reading and writing.

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.

Return Value

A FILE 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: The function fopen should only be used to open a file whose file pointer will be input to a 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.stat.fopen import fopen
from pyimsl.stat.fclose import fclose
from pyimsl.stat.writeMatrix import writeMatrix
from pyimsl.stat.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")
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