CFile:Write
The Write method writes binary data to a file opened in binary mode. The format to use for writing the value or table elements is specified by the sPixelType parameter. Also see the WriteBswap method for writing data with a reversed byte order.
nBytes = CFile:Write( value, sPixelType )
nBytes = CFile:Write( table, sPixelType* )
value is a value to be written to the file.
table is a 1-dimensional array. The array cannot contain nested tables and must have sequential indices through the number of elements in the array.
sPixelType is a string that specifies the data format used to write the value or the table elements to the file. When writing a table, sPixelType is optional and defaults to "double" (i.e., 64-bit real values) if not specified.
The return value nBytes is the number of bytes written to the file.
This method can write single values or an entire data table to the CFile object. The format used in the output file is not necessarily the same as that of the value being written. For example, suppose value = 12. Then you can write this value as an 8-, 16-, or 32-bit integer or a 32- or 64 bit real number by changing the sPixelType string. See Pixel Types for a description of the available pixel types and the string to use for sPixelType.
The script below provides a complete function to save a 1-dimensional array to a raw binary file as 64-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 inside the function are defined as "local" because they are use only inside the function.
|
|
|
-- failure: a table was not passed |
|
-- Get a file name using the FileOpen dialog |
|
-- check that a file name was selected |
|
-- Open the file for writing binary data |
|
-- if the file was opened |
|
-- number of columns as a long integer |
|
-- number of rows as a long integer |
|
-- 6 = code for 64-bit real number data |
|
-- write the table as 64-bit real values. |
|
-- remember to close the file when done |
|
-- success |
|
|
|
|
|
-- failure |
|
|
CFile Class, WriteBswap, Read, File Access Modes, GetFileName
Mira Pro x64 Script User's Guide, Copyright Ⓒ 2024 Mirametrics,
Inc. All Rights Reserved.