MEL Week 10

File I/O (p. 181)
 
 
 
global proc writeArray( string $fname, float $array[] ) {
 
int $fh = `fopen $filename "w"`;
 
if ($fh == 0) {
        error( "Unable to open file \"" + $filename + "\" for writing." );
        return;
}
 
float $v;
 
for ($v in $array) fwrite $fh $v;
 
fclose $fh;
 
}
 
 
 
 
global proc readArray( string $fname, float $array[] ) {
 
int $fh = `fopen $filename "r"`;
 
if ($fh == 0) {
        error( "Unable to open file \"" + $filename + "\" for reading." );
        return;
}
 
float $v;
 
for ($v in $array) $v = fread $fh $v;
 
fclose $fh;
 
}
 
 
 

Synopsis
fopen <string> [ <string> ]

ReturnValue
int

Description

This command defaults to opening a file for writing and returns a file identifier.

The optional mode string may be one of the following:

"w" open file for writing (destroys prior contents of file).
"a" append for writing (appends to end of file).
"r" open for reading.
A "+" optionally after the mode character opens for both reading and writing.

If an error occurrs while attempting to open the file, fopen will return zero. For more information see the C implementation of the function fopen.
Examples

$exampleFileName = ( `internalVar -userTmpDir` + "example.tmp" );
$fileId=`fopen $exampleFileName "w"`;
fwrite $fileId "Hello there\n";
fclose $fileId;


Synopsis
fwrite <int> <int | float | string | vector>

ReturnValue
None

Description

The fwrite command writes the data argument in binary format to a file. Strings are written as ascii terminating with a NULL character. Note that this should not be used for writing to a text file. If you wish to write out a text file, use fprint instead as it does not terminate the written string with a null character. Only use this for binary files.

 

 

Synopsis
fprint <int> <int | float | string | vector>

ReturnValue
None

Description

This command is functionally equivalent to the print function. The difference being the mandatory file identifier argument.

Flags
None

Examples

// Print a string into a file
//
$exampleFileName = ( `internalVar -userTmpDir` + "example.tmp" );
$fileId=`fopen $exampleFileName "w"`;
fprint $fileId "Hello there\n";
fclose $fileId;
 
 

Synopsis
fread <int> <int | float | string | vector>

ReturnValue
int | float | string | vector

Description

Reads the next set of bytes as binary data interpreting their type by the dummy argument. An integer dummy argument, for instance, tells fread that it should read and return an integer value. Strings are read up to the first occurrence of a NULL character or until EOF. There is a read limit of 1024 bytes on strings. If a string being read is longer than 1024 bytes, then only the first 1024 bytes will be returned. The type returned by the fread command corresponds to the type of the second argument.

Examples

// Make a sample file to use as a test example
//
$exampleFileName = ( `internalVar -userTmpDir` + "example.tmp" );
$fileId=`fopen $exampleFileName "w"`;
fwrite $fileId "Hello there\n";
fclose $fileId;

// Now, open the file and read the data
//
$fileId=`fopen $exampleFileName "r"`;
string $s;
$s=`fread $fileId $s`;
print( $s + "\n" );
fclose $fileId;

 
 
 

Synopsis
fgetline <int>

ReturnValue
string

Description

Returns the next line (string of characters followed by a newline) or nothing if end of file is reached.

Examples

// Make a sample file to use as a test example
//
$exampleFileName = ( `internalVar -userTmpDir` + "example.tmp" );
$fileId=`fopen $exampleFileName "w"`;
fwrite $fileId "Hello there\nMEL user\n";
fclose $fileId;

// Now read it back one line at a time
//
$fileId=`fopen $exampleFileName "r"`;
string $nextLine = `fgetline $fileId`;
while ( size( $nextLine ) > 0 ) {
print ( $nextLine );
$nextLine = `fgetline $fileId`;
}
fclose $fileId;