new_fileopen


This global function opens a file and returns a new CFile object for accessing the file.In contrast, the new_file function simply creates a new CFile object.

Syntax

CFile_obj = new_fileopen( sPathName, sMode )

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

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

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

Remarks

This function can open the file in either binary or text mode for reading, writing, or appending file data. The modes are described under File Access Modes. Also see the functions in the lua I/O library.

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 data array (lua indexed table) to a binary file. The data 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. FInally, the data array is saved. When using Write for a table, the output pixel type defaults to 64-bit real values if it is not specified. The values sName and F are declared as local inside the function to avoid a collision with the same name outside the function.

function WriteDataArray( t, nCols, nRows )

 

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

-- bail out if a table was not passed

  local sPathName = GetFileName()

-- get the full path name

  if sPathName ~= nil then

-- check that a file name was selected

    local F = new_fileopen( sPathName, "wb" )

-- Open the file and create a CFile object

    if F ~= nil then

-- if the file was opened, write values:

      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 = the code for 64-bit real number data

      F:Write( t )

-- write the data array

      F:Close()

-- close the file when done

      return true

-- return success

    end

 

  end

 

  return false

-- reurn failure

end

 

Related Topics

CFile Class

new_file

CFile:Open

File Access Modes

GetFileName

lua I/O library


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