FileOpen


This global function opens a file and returns a new CFile object for accessing the file. This function is not a class member but it provides an alternative to using CFile:new followed by the class Open method. This method creates and returns the CFile object for the file.

Syntax

CFile = FileOpen( sFileName, sMode )

bullet.gif    sFileName is the full path name for the file to be opened.

bullet.gif    sMode is the file mode (see Remarks, below).

bullet.gif    CFile is a new CFile object returned by this function. On failure, nil is returned.

Remarks

This function can open the file in either text or binary mode for reading, writing, or appending. The modes and how to specify them are described under CFile Access Modes.

When the script is finished with the file, the CFile object created by this function should closed using either the Close or delete destructor.

Example

The script below provides a complete function to save a 1-dimensional array to a raw binary file as 16-bit real numbers. The array has indices [1] through [ nCols x nRows ].

The nCols and nRows parameters are written individually as "long" integers (32-bit). Following that is a numeric Pixel Type code to indicate the table contains 64-bit real numbers. Then the entire table is saved. When using Write for a table, the output pixel type defaults to 64-bit real values if it is not specified. For the file to have the correct length, it is assumed that the number of table elements is nCols x nRows. The values sName and F are declared as local inside the function to avoid a collision with the same name outside the function.

function Write2dTable( t, nCols, nRows )

 

  if type(t) ~= "table" then return false end

-- bail out if a table was not passed

  local sName = GetFileName()

-- open a file using the FileOpen dialog

  if sName ~= nil then

-- check that a file name was selected

    local F = FileOpen( sName, "wb" )

-- Open the file for writing binary data

    if F ~= nil then

-- if the file was opened

      F:Write( nCols, "long" )

-- number of columns as a long integer

      F:Write( nRows, "long" )

-- number of rows as a long integer

      F:Write( 6, "long" )

-- 6 = code for 64-bit real number data

      F:Write( t )

-- write the table as 64-bit real values.

      F:Close()

-- remember to close the file when done

      return true

-- success

    end

 

  end

 

  return false

-- failure

end

 

Related Topics

CFile Class

new

Open

File Access Modes

GetFileName


Mira Pro x64 Script User's Guide, Copyright Ⓒ 2023 Mirametrics, Inc. All Rights Reserved.