aqFile.OpenBinaryFile Method

Applies to TestComplete 15.69, last modified on November 13, 2024

Description

The OpenBinaryFile method locates the specified file and opens it for reading and/or writing in binary mode.

Declaration

aqFile.OpenBinaryFile(Path, FileAccess, OverwriteOrCreate)

Path [in]    Required    String    
FileAccess [in]    Required    Integer    
OverwriteOrCreate [in]    Optional    Boolean Default value: False   
Result An aqBinaryFile object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

Path

Specifies the fully-qualified path to the desired file.

FileAccess

Defines the file access type. The following constants and values are possible:

Constant Value Description
aqFile.faWrite 10 Open the file for writing.
aqFile.faRead 11 Open the file for reading.
aqFile.faReadWrite 12 Open the file both for reading and writing.

OverwriteOrCreate

A Boolean value that specifies method behavior depending on whether the file exists or not and on the specified FileAccess parameter:

File already exists
Parameter Value aqFile.faRead aqFile.aqReadWrite,
aqFile.Write
True The method will not be able to overwrite the file opened for reading only and will fail. The method will open the file for writing or for reading and writing and overwrite the file contents.
False The method will open the file for reading. The method will open the file for writing or for reading and writing and leave the file contents.
File does not exist
Parameter Value aqFile.faRead aqFile.aqReadWrite,
aqFile.Write
True The method will not be able to create the file and will fail. The method will create the file and will open it for writing or for reading and writing.
False The method will fail. The method will fail.

Result Value

An aqBinaryFile object that corresponds to the specified file. To perform operations over the opened file, use the methods and properties of the returned object.

Remarks

Once the file is opened only for reading, the aqTextFile.Cursor property of the corresponding object is positioned at the beginning of the file. If the file is opened for writing, or both for reading and writing, the property is positioned at the end of the file. To start working with the file, you should set this property to point to the beginning of the file (assign to zero) or to any desired position within the file.

Example

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

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";

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

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

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

// After this routine is executed, the "A 2 { " string is written to the end of the FileName.txt file

Python

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

# After this routine is executed, the "A 2 { " string is written to the end of the FileName.txt file

VBScript

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

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

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

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

' After this routine is executed, the "A 2 { " string is written to 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.Write('A');
  myFile.Write('2');
  myFile.Write(123);

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

// After this routine is executed, the "A 2 { " string is written to the end of the FileName.txt file

C++Script, C#Script

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

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

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

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

// After this routine is executed, the "A 2 { " string is written to 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
OpenTextFile Method
aqBinaryFile Object
Close Method

Highlight search results