10.3 File Object

Contains methods and properties to perform various I/O operations on the opened file.

10.3.1 Eof property

Reaches the end of the open file.

Syntax

 object.Eof
 

Type

Boolean.

Attributes

Read-only.

Example

This example returns TRUE when the end of text.txt is reached.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open("sys:temp\text.txt", FIO_CREATE_WRITE) 
 print fopen.eof 
 fopen.Close()
 

See Also

10.3.2 Name property

Returns the name of the open file.

Syntax

 object.Name
 

Type

String.

Attributes

Read-only.

Example

This example returns the name of the open file (text.txt).

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open("sys:temp\text.txt", FIO_CREATE_WRITE) 
 print fopen.Name 
 fopen.Close()
 

10.3.3 Position property

Returns the current position of the open file.

Syntax

 object.Position
 

Type

Long.

Attributes

Read-only.

Example

This example gives the current file position of text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open("sys:temp\text.txt", FIO_CREATE_WRITE) 
 print fopen.Position 
 fopen.Close()
 

10.3.4 Close method

Closes the open file. If Close is not called, the object destructor will close the file handle.

Syntax

 object.Close()
 

Parameters

None.

Return Values

Boolean. Returns TRUE if the file is closed successfully; otherwise, FALSE.

Example

This example closes the open file (text.txt).

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open("sys:temp\text.txt", FIO_CREATE_WRITE) 
 fopen.Close()
 

See Also

10.3.5 ReadByte method

Reads the number of bytes from the current position in the file.

Syntax

 object.ReadByte(
    [countBytes As UnsignedLong])
 

Parameters

countBytes
Optional. The number of bytes to be read (default=1 byte).

Return Values

Byte[ ]. Returns 0 if the read fails.

Example

This example reads 4 bytes from the current position in the file text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.Open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.Write("A", 1, "B", "D") 
 ’Set the cursor to the beginning of the file 
 fopen.Seek(SEEK_SET) 
 a = fopen.ReadByte(4) 
 if ( Err.Number = 0 ) then 
      For I = 0 to Len(a) - 1 
           Print(a(I)) 
      Next 
 else 
      print("Failed to read from file " & Err.Description) 
 endif 
 fopen.Close()
 

10.3.6 ReadInt method

Reads an integer from the file.

Syntax

 object.ReadInt()
 

Parameters

None.

Return Values

Integer. Returns 0 if the read fails.

Example

This example reads an integer from the file text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.Write(123) 
 ’Set the cursor to the beginning of the file 
 fopen.Seek(SEEK_SET) 
 a = fopen.ReadInt 
 if ( Err.Number = 0 ) then 
           print(a) 
 else 
      print("Failed to read from file " & Err.Description) 
 endif 
 fopen.Close()
 

10.3.7 ReadReal method

Reads a real number from the file.

Syntax

 object.ReadReal()
 

Parameters

None.

Return Values

Real. Returns 0 if the read fails.

Example

This example reads a real number from the file text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.write(123.123) 
 ’Set the cursor to the beginning of the file 
 fopen.Seek(SEEK_SET) 
 a = fopen.ReadReal 
 if ( Err.Number = 0 ) then 
           print(a) 
 else 
      print("Failed to read from file " & Err.Description) 
 endif 
 fopen.Close()
 

10.3.8 ReadString method

Reads a string from the file until the first carriage return is encountered.

Syntax

 object.ReadString()
 

Parameters

None.

Return Values

String. Returns 0 if the read fails.

Example

This example reads a string from the file text.txt until the first carriage return is encountered.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.write("Test string") 
 ’Set the cursor to the beginning of the file 
 fopen.Seek(SEEK_SET) 
 a = fopen.ReadString 
 if ( Err.Number = 0 ) then 
           print(a) 
 else 
      print("Failed to read from file " & Err.Description) 
 endif 
 fopen.Close()
 

10.3.9 Seek method

Moves the file pointer.

Syntax

 object.Seek(
    [origin As Integer 
    [,countBytes As Long)
 

Parameters

origin
Optional. See Position Constants.
countBytes
Optional. The number of bytes to offset.

Return Values

Boolean. Returns TRUE if successful; otherwise, FALSE.

Remarks

By default, this method moves the file pointer by one byte from the current position.

Example

This example moves the file pointer to the beginning of the file.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.write("Test string") 
 ’Set the cursor to the beginning of the file 
 fopen.seek(SEEK_SET) 
 a = fopen.ReadString 
 print(a) 
 fopen.Close()
 

10.3.10 Write method

Overwrites the file with the specified data.

Syntax

 object.Write(
    data1 As AnyType 
    [,dataN As AnyType])
 

Parameters

data1
The data to be written into the file. This data can be of any type.
dataN
Optional. The data to be written into the file. This data can be of any type.

Return Values

Boolean. Returns TRUE if successful; otherwise, FALSE.

Remarks

This method can take up to 15 parameters. Multiple parameters are separated by commas.

Example

This example writes "Meet George Jetson" in the file text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.write("Meet George Jetson") 
 ’Set the cursor to the beginning of the file 
 fpen.Close()
 

10.3.11 WriteByte method

Writes the specified number of bytes to the file from the buffer.

Syntax

 object.WriteByte(
    Buffer As Byte 
    [,countBytes As Integer])
 

Parameters

Buffer
The contents of this buffer will be written to the file.
countBytes
Optional. The number of bytes to be written to the file. If not specified, the contents of the entire buffer will be written to the file.

Return Values

Boolean. Returns TRUE if successful; otherwise, FALSE.

Example

This example writes the entire contents of "Writing buffer" to the file text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.writeByte("Writing buffer") 
 ’Set the cursor to the beginning of the file 
 fopen.Close()
 

10.3.12 WriteLine method

Writes a line of data to the file.

Syntax

 object.WriteLine(
    data1 As AnyType 
    [,dataN As AnyType])
 

Parameters

data1
The data to be written into the file. This data can be of any type.
dataN
Optional. The data to be written into the file. This data can be of any type.

Return Values

Boolean. Returns TRUE if successful; otherwise, FALSE.

Remarks

This method can take up to 15 parameters. Multiple parameters are separated by commas.

WriteLine is similar to the Write method, except WriteLine adds a newline character at the end.

Example

This example writes "First line" and "Second line" to the file text.txt.

 Set fio = CreateObject("ucx:fio") 
 Set fopen = fio.open ("sys:temp\text.txt", FIO_CREATE_UPDATE) 
 fopen.writeLine("First line") 
 fopen.writeLine("Second line") 
 ’Set the cursor to the beginning of the file 
 fopen.Close()