Chapter 3 DynaScript Predefined Objects


file properties

The file object has these properties:

eof property

Syntax

file.eof 

Attributes

This property is read-only.

Description

Set to true when an input operation attempts to read past the end of the file.

Example

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();
}
-->

errorNumber property

Syntax

file.errorNumber 

Attributes

This property is read-only.

Description

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.

Example

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." );
}
-->

errorString property

Syntax

file.errorString 

Attributes

This property is read-only.

Description

A string containing an error message for the error code of the last file method called.

Example

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

name property

Syntax

file.name

Description

Name of the file to be manipulated (string).

Example

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

mode property

Description

The access mode with which the file was opened:

Access mode

Description

r

Open for read-only.

w

Open for write, the file length to zero.

a

Open for write at end of file.

rb

Open binary file for reading.

rt

Open text file for reading.

wb

Create binary file for writing.

wt

Create text file for writing.

ab

Open binary file for write at end of file.

at

Open text file for write at end of file.

r+

Open file for update (reading/writing).

w+

Create file for update (reading/writing).

a+

Open file for update at end of file.

rb+

Open binary file for update (reading/writing).

wb+

Create binary file for update.

ab+

Open binary file for update at end of file.

rt+

Open text file for update (reading/writing).

wt+

Create text file for update (reading/writing).

at+

Open text file for update at end of file.

Note  

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.

Syntax

file.mode

Example

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.