MEL Week 10
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.
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
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;
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;