Chapter 3 DynaScript Predefined Objects
The file
object
has these properties:
file.eof
This property is read-only.
Set to true when an input operation attempts to read past the end of the file.
This example reads each character in the file input.txt:
<!--SCRIPT
inputFile = new File( "d:\\test\\input.txt", "r" );
ch = inputFile.ReadChar ();
while ( ! inputFile.eof ) {
ch = inputFile.ReadChar();
}
-->
file.errorNumber
This property is read-only.
A number representing the error code of the
last file method called. If an error occurred the errorNumber
will
be non-zero. The values for errorNumber
and errorString
are
system dependent and may differ between NT and versions of UNIX.
This example displays the error number that results from opening a file in one mode and then opening it again in a different mode without closing the first instance:
<!--SCRIPT
inputFile = new File( "d:\\test\\input.txt", "r" );
ch = inputFile.ReadChar ();
while ( ! inputFile.eof ) {
ch = inputFile.ReadChar();
document.WriteLn( ch );
}
inputFile = new File( "d:\\test\\input.txt", "w" );
document.WriteLn( "The error number is: " + inputFile.errorNumber );
-->
This example checks for an error code of 0 and returns a message if a new file is created successfully:
<!--SCRIPT
//Common Values for errorNumber on NT:
//0 No error
//1 No such file or directory
//6 Permission denied
var fileName = "c:\\foo.txt";
myFile = new File( fileName, "rt" );
if( myFile.errorNumber != 0 ) {
// if error was encountered:
document.writeln( "Error number " + myFile.errorNumber + " was encountered: " + myFile.errorString );
} else {
document.writeln( myFile.name + " accessed successfully in " + myFile.mode + " mode." );
}
-->
file.errorString
This property is read-only.
A string containing an error message for the error code of the last file method called.
This example displays an error message if there is a problem with the first file method called:
<!--SCRIPT
inputFile = new File( "d:\\test\\input5.txt","r" );
document.WriteLn( inputFile.errorString );
line = inputFile.ReadLine();
document.WriteLn( inputFile.GetFilePtr() );
inputFile.Close();
-->
If the file input5.txt cannot be found in the specified directory, the output looks like:
No such file or directory
null
file.name
Name of the file to be manipulated (string).
This example displays the name of the file
represented by the file object myFile
:
<!--SCRIPT
myFile = new File( "d:\\test\\input.txt", "r" );
document.WriteLn( myFile.name );
-->
The output from this example is:
input.txt
The access mode with which the file was opened:
Changing a file mode
Simply changing the file mode does
not change the mode of the currently opened file. A call to Close
followed by a call to Open is required to change a file mode.
file.mode
This example opens the file output.txt and appends the phrase "hello world" to the end of the file:
<!--SCRIPT
outputFile = new File ( "d:\\test\\output.txt", "a" );
outputFile.Write( "hello world" );
outputFile.Close();
-->
Copyright © 1999 Sybase, Inc. All rights reserved. |