OpenBinaryFile Method

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

Description

The OpenBinaryFile method opens the related file for reading and/or writting in binary mode.

Declaration

aqFileInfoObj.OpenBinaryFile(FileAccess)

aqFileInfoObj An expression, variable or parameter that specifies a reference to an aqFileInfo object
FileAccess [in]    Required    Integer    
Result An aqBinaryFile object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

FileAccess

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

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

Result Value

An aqBinaryFile object that corresponds to the given file. To perform operations over the opened file, use 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 = aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 = aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 WriteTextToFile
  Dim Path, myFile
  Path = "C:\MyFiles\FileName.txt"

  ' Opens the file for writing
  Set myFile = aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 := aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 = aqFileSystem["GetFileInfo"](Path)["OpenBinaryFile"](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 = aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 = aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 = aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 := aqFileSystem.GetFileInfo(Path).OpenBinaryFile(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 = aqFileSystem["GetFileInfo"](Path)["OpenBinaryFile"](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