aqFile.OpenBinaryFile Method

Applies to TestComplete 12.60, last modified on September 17, 2018

Description

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

Declaration

aqFile.OpenBinaryFile(PathFileAccessOverwriteOrCreate)

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

Indicates what a method should do if the file to be opened does not exist or already exists.

If the specified file does not exist and this parameter is True, the method will create a new file at the specified location and open it. Otherwise, a new file will not be created and the method will fail to open the specified file.

If the file already exists on the disk and this parameter is True, the method will overwrite that file; otherwise it will not.

If the specified file is read-only and this parameter is True, the method will raise an exception.

By default, the method does not create new files nor overwrites existing files.

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
aqFile.OpenTextFile Method
aqBinaryFile Object
Close Method

Highlight search results