Cursor Property

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

Use the Cursor property to read the current position or to set a new position within a text file.

Declaration

aqTextFileObj.Cursor

Read-Write Property Integer
aqTextFileObj An expression, variable or parameter that specifies a reference to an aqTextFile object

Applies To

The property is applied to the following object:

Property Value

A zero-based integer that denotes a position in a file.

Remarks

After the file is opened, the Cursor property points to the end or to the beginning of the file. If the file is open for reading, Cursor points to the beginning of the file. If the file is open for writing or appending, Cursor points to the end of the file (the cursor is positioned right after the last byte of the file’s content).

Example

The code below reads the content of the specified file and posts it by letters to the test log.

JavaScript, JScript

function ReadFile()
{
  var sPath = "D:\\Files\\MyFile.txt";
  
  // Opens the specified file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  
  // Reads the first line and posts it by letters to the test log
  while (! myFile.IsEndOfFile() )
  {
    // Obtains the letter and its number
    var sLet = myFile.ReadString(1);
    var lNum = myFile.Cursor;
    Log.Message("The " + lNum + " letter is:");
    Log.Message(sLet);
  }
  
  // Closes the file
  myFile.Close();
}

Python

def ReadFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Opens the specified file for reading
  myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  # Reads the first line and posts it by letters to the test log 
  while not myFile.IsEndOfFile():
    # Obtains the letter and its number
    sLet = myFile.ReadString(1)
    lNum = myFile.Cursor
    Log.Message("The " + str(lNum) + " letter is:")
    Log.Message(sLet)
  # Closes the file
  myFile.Close()

VBScript

Sub ReadFile
  
  sPath = "D:\Files\MyFile.txt"
  
  ' Opens the specified file for reading
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  
  ' Reads the first line and posts it by letters to the test log
  While Not myFile.IsEndOfFile
    ' Obtains the letter and its number
    sLet = myFile.ReadString(1)
    lNum = myFile.Cursor
    Log.Message("The " & lNum & " letter is:")
    Log.Message(sLet)
  WEnd
  
  ' Closes the file
  myFile.Close
  
End Sub

DelphiScript

function ReadFile;
var sPath, myFile, sLet, lNum;
begin
  sPath := 'D:\Files\MyFile.txt';
  
  // Opens the specified file for reading
  myFile := aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  
  // Reads the first line and posts it by letters to the test log
  while (not myFile.IsEndOfFile() ) do
  begin
    // Obtains the letter and its number
    sLet := myFile.ReadString(1);
    lNum := myFile.Cursor;
    Log.Message('The ' + IntToStr(lNum) + ' letter is:');
    Log.Message(sLet);
  end;
  
  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

function ReadFile()
{
  var sPath = "D:\\Files\\MyFile.txt";
  
  // Opens the specified file for reading
  var myFile = aqFile["OpenTextFile"]( sPath, aqFile["faRead"], aqFile["ctANSI"] );
  
  // Reads the first line and posts it by letters to the test log
  while (! myFile["IsEndOfFile"]() )
  {
    // Obtains the letter and its number
    var sLet = myFile["ReadString"](1);
    var lNum = myFile["Cursor"];
    Log["Message"]("The " + lNum + " letter is:");
    Log["Message"](sLet);
  }
  
  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
OpenBinaryFile Method
SetPosition Method
Line Property
Column Property

Highlight search results