aqBinaryFile Object

Applies to TestComplete 14.71, last modified on April 22, 2021

Description

The aqBinaryFile object provides programming access to an appropriate binary file. The objects of this type are returned by the OpenBinaryFile method.

Members

Example

The sample routines below demonstrate how you can read data from and write it to binary files using the aqBinaryFile object.

The following script routine opens an existing file in binary mode and then writes text to it:

JavaScript, JScript

function WriteTextToFile()
 {
   var Path = "C:\\MyFiles\\FileName.txt";

   // Open the file for writing
   var myFile = aqFile.OpenBinaryFile(Path, aqFile.faWrite);

   // Write text to the file
   myFile.WriteString("A");
   myFile.WriteString("2");
   myFile.WriteByte(123);

   // Close the file
   myFile.Close();
 }

// After this routine is executed, the "A 2 { " string will appear at the end of the FileName.txt file.

Python

def WriteTextToFile():
  Path = "C:\\MyFiles\\FileName.txt"
  
  # Open the file for writing
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faWrite)
  
  # Write text to the file
  myFile.WriteString("A")
  myFile.WriteString("2")
  myFile.WriteByte(123)
  
  # Close the file
  myFile.Close()

# After this routine is executed, the "A 2 { " string will appear at the end of the FileName.txt file.

VBScript

Sub WriteByteToFile
   Dim Path, myFile
   Path = "C:\MyFiles\FileName.txt"

   ' Open the file for writing
   Set myFile = aqFile.OpenBinaryFile(Path, aqFile.faWrite)

   ' Write data to the file
   Call myFile.WriteString("A")
   Call myFile.WriteString("2")
   Call myFile.WriteByte(123)

   ' Close the file
   Call myFile.Close()
End Sub

' After this routine is executed, the "A 2 { " string will appear at the end of the FileName.txt file.

DelphiScript

procedure WriteTextToFile;
var Path, myFile;
begin
   Path := 'C:\MyFiles\FileName.txt';

   // Opens the file for writing
   myFile := aqFile.OpenBinaryFile(Path, aqFile.faWrite);

   // Writes text to the file
   myFile.WriteString('A');
   myFile.WriteString('2');
   myFile.WriteByte(123);

   // Closes the file
   myFile.Close();
end;

// After this routine is executed, the "A 2 { " string will appear at the end of the FileName.txt file.

C++Script, C#Script

function WriteTextToFile()
 {
   var Path = "C:\\MyFiles\\FileName.txt";

   // Open the file for writing
   var myFile = aqFile["OpenBinaryFile"](Path, aqFile.faWrite);

   // Write text to the file
   myFile["WriteString"]("A");
   myFile["WriteString"]("2");
   myFile["WriteByte"](123);

   // Close the file
   myFile["Close"]();
 }

// After this routine is executed, the "A 2 { " string will appear at the end of the FileName.txt file.

The following script routine demonstrates how you can read data from a file:

JavaScript, JScript

function ReadByteFromFile()
{
  var Path = "C:\\MyFiles\\FileName.txt";

  // Opens the specified file for reading
  var myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead);
  Log.Message("File by bytes:");

  // Reads bytes from the file and posts them to the test log
  while(! myFile.IsEndOfFile())
  {
    s = myFile.ReadByte();
    Log.Message(s);
  }

  // Closes the file
  myFile.Close();
}

Python

def ReadByteFromFile():
  Path = "C:\\MyFiles\\FileName.txt"
  # Opens the specified file for reading 
  myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead)
  Log.Message("File by bytes:")
  # Reads bytes from the file and posts them to the test log
  while not myFile.IsEndOfFile():
    s = myFile.ReadByte()
    Log.Message(s)
  # Closes the file 
  myFile.Close()

VBScript

Sub ReadByteFromFile
  Dim Path
  Path = "C:\MyFiles\FileName.txt"

  ' Opens the specified file for reading
  Set myFile = aqFile.OpenBinaryFile(Path, aqFile.faRead)
  Log.Message("File by bytes:")

  ' Reads bytes the file and posts them to the test log
  While Not myFile.IsEndOfFile()
    s = myFile.ReadByte()
    Log.Message(s)
  WEnd

  ' Closes the file
  Call myFile.Close()
End Sub

DelphiScript

procedure ReadBytetFromFile;
var Path, myFile, s;
begin
  Path := 'C:\MyFiles\FileName.txt';

  // Opens the specified file for reading
  myFile := aqFile.OpenBinaryFile(Path, aqFile.faRead);
  Log.Message('File by bytes:');

  // Reads bytes from the file and posts them to the test log
  while not myFile.IsEndOfFile() do
    begin
      s := myFile.ReadByte();
      Log.Message(s);
    end;

  // Closes the file
  myFile.Close();
end;

C++Script, C#Script

function ReadByteFromFile()
{
  var Path = "C:\\MyFiles\\FileName.txt";

  // Opens the specified file for reading
  var myFile = aqFile["OpenBinaryFile"](Path, aqFile.faRead);
  Log["Message"]("File by bytes:");

  // Reads bytes from the file and posts them to the test log
  while(! myFile.IsEndOfFile())
  {
    s = myFile["ReadByte"]();
    Log["Message"](s);
  }

  // Closes the file
  myFile["Close"]();
}

See Also

Working With Files From Scripts
aqFile Object
OpenBinaryFile Method

Highlight search results