ReadString Method

Applies to TestComplete 15.31, last modified on March 17, 2022

Description

The ReadString method retrieves the specified number of characters from a file starting from the current cursor position.

Declaration

aqTextFileObj.ReadString(NSymbols)

aqTextFileObj An expression, variable or parameter that specifies a reference to an aqTextFile object
NSymbols [in]    Required    Integer    
Result String

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

NSymbols

Specifies the number of characters to retrieve.

Result Value

The text fragment read from the file.

Remarks

On success, the cursor position is shifted by the number of characters read.

Note that when reading from the beginning of a Unicode file, the ReadString method skips the file’s byte-order mask, that is, the first helper characters of the file that indicate the encoding type -- UTF-8 or UTF-16. The byte-order mask of the UTF-8 encoding consists of 3 characters and UTF-16 is 2 characters long.

Example

The code below reads the specified file by lines and posts these lines to the test log.

JavaScript, JScript

function ReadTextFromFile()
{
  var sPath = "D:\\Files\\MyFile.txt";

  // Creates a text file at the specified location
  aqFile.Create(sPath);

  // Opens the created file for reading
  var myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  myFile.SetPosition(0, 0);
  
  // Obtains the total number of lines in the file
  var lNum = myFile.LinesCount;
  
  for (var i = 0; i < lNum; i++)
  {
    // Obtains the current line's length
    var lLength = myFile.LineLength(i);
    // Reads the current line
    var Line = myFile.ReadString(lLength);
    // Sets the cursor to the next line
    myFile.SetPosition(i+1, 0);
    // Posts the current line to the test log
    Log.Message(Line);
  }
  
  // Closes the file
  myFile.Close();
}

Python

def ReadTextFromFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Creates a text file at the specified location 
  aqFile.Create(sPath)
  # Opens the created file for reading 
  myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  myFile.SetPosition(0, 0)
  # Obtains the total number of lines in the file
  lNum = myFile.LinesCount
  for i in range(0, lNum):
    # Obtains the current line's length
    lLength = myFile.LineLength(i)
    # Reads the current line 
    Line = myFile.ReadString(lLength)
    # Sets the cursor to the next line
    myFile.SetPosition(i+1, 0)
    # Posts the current line to the test log
    Log.Message(Line)
  # Closes the file 
  myFile.Close()

VBScript

Sub ReadTextFromFile
  Dim sPath
  sPath = "D:\Files\MyFile.txt"

  ' Creates a text file at the specified location
  aqFile.Create(sPath)

  ' Opens the created file for reading
  Set myFile = aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI)
  Call myFile.SetPosition(0, 0)
  
  ' Obtains the total number of lines in the file
  lNum = myFile.LinesCount
  
  For i = 0 to lNum-1
    ' Obtains the current line's length
    lLength = myFile.LineLength(i)
    ' Reads the current line
    Line = myFile.ReadString(lLength)
    ' Sets the cursor to the next line
    Call myFile.SetPosition(i+1, 0)
    ' Posts the current line to the test log
    Log.Message(Line)
  Next
  
  ' Closes the file
  Call myFile.Close()
End Sub 

DelphiScript

function ReadTextFromFile;
var sPath, myFile, lNum, i, lLength, Line;
begin
  sPath := 'D:\Files\MyFile.txt';

  // Creates a text file at the specified location
  aqFile.Create(sPath);

  // Opens the created file for reading
  myFile := aqFile.OpenTextFile(sPath, aqFile.faRead, aqFile.ctANSI);
  myFile.SetPosition(0, 0);
  
  // Obtains the total number of lines in the file
  lNum := myFile.LinesCount;
  
  for i := 0 to lNum-1 do
  begin
    // Obtains the current line's length
    lLength := myFile.LineLength(i);
    // Reads the current line
    Line := myFile.ReadString(lLength);
    // Sets the cursor to the next line
    myFile.SetPosition(i+1, 0);
    // Posts the current line to the test log
    Log.Message(Line);
  end;
  
  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

function ReadTextFromFile()
{
  var sPath = "D:\\Files\\MyFile.txt";

  // Creates a text file at the specified location
  aqFile["Create"](sPath);

  // Opens the created file for reading
  var myFile = aqFile["OpenTextFile"](sPath, aqFile.faRead, aqFile.ctANSI);
  myFile["SetPosition"](0, 0);
  
  // Obtains the total number of lines in the file
  var lNum = myFile["LinesCount"];
  
  for (var i = 0; i < lNum; i++)
  {
    // Obtains the current line's length
    var lLength = myFile["LineLength"](i);
    // Reads the current line
    var Line = myFile["ReadString"](lLength);
    // Sets the cursor to the next line
    myFile["SetPosition"](i+1, 0);
    // Posts the current line to the test log
    Log["Message"](Line);
  }
  
  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
Write Method
WriteLine Method
ReadLine Method
ReadToSymbol Method
ReadAll Method

Highlight search results