Datasheet
17
directory path is declared, Maya assumes that note.tmp resides in the project directory. The
$valueToWrite string is constructed from the variable $test, which is defined outside the pro-
cedure, and
“\n”. The \n instructs fprint to include an end-of-line code. This forces fprint
to create a brand-new line the next time the procedure is called. If \n is not used, fprint
will continually append to the same line. You can also use
\n when creating the original file,
like so:
fprint $fileWrite “Maya Rules\n”;
fprint does not write to the disk each time it’s called. Instead, it writes to a temporary
software buffer. When the buffer is full or the
fclose command is used, fprint writes all the
information to the disk at one time. You can force
fprint to write to the disk at any time by
inserting the
fflush command.
To read and print the contents of a file, one line at a time, you can use this code:
$fileRead = `fopen $fileName “r”`;
string $readLine = `fgetline $fileRead`;
while ( size($readLine) > 0) {
print ( $readLine );
$readLine = `fgetline $fileRead`;
}
fclose $fileRead;
Reading Text Files
As an alternative to fopen, the popen command allows you to read regular text files by piping
a system function. For example, you can use the following code to read and print each line of
a 50-line text file:
int $count = 0; int $lineNumber = 50;
$pipe = popen( “type note.txt”, “r” );
if ($count < $lineNumber) {
string $readLine = `fgetline $pipe`;
string $line = ( $readLine );
print $line;
$count = $count + 1;
}
pclose ( $pipe );
Passing Information to Files
07405c01.indd 1707405c01.indd 17 1/17/07 8:30:56 PM1/17/07 8:30:56 PM