OpenTextFile Method

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

Description

The OpenTextFile method opens the related file for reading and/or writting in text mode using the given encoding type.

Declaration

aqFileInfoObj.OpenTextFile(FileAccessTextCodingType)

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

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

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.

TextCodingType

An integer value that defines a file’s character encoding. You can specify one of the possible integer values directly, or you can use the appropriate constant:

Constant Value Description
aqFile.ctANSI 20 ANSI
aqFile.ctUnicode 21 Unicode (UTF-16)
aqFile.ctUTF8 22 UTF8

Result Value

An aqTextFile object that corresponds to the 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 text files using the OpenTextFile method.

The following script routine creates a new text file and then writes text to it:

JavaScript, JScript

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

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

  // Opens the created file for writing
  var myFile = aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile.WriteLine("line1");
  myFile.WriteLine("line2");
  myFile.WriteLine("line3");

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

Python

def WriteTextToFile():
  sPath = "D:\\Files\\MyFile.txt"
  # Creates a text file at the specified location 
  aqFile.Create(sPath)
  # Opens the created file for writing 
  myFile = aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faWrite, aqFile.ctUnicode)
  
  # Writes text to the file 
  myFile.WriteLine("line1")
  myFile.WriteLine("line2")
  myFile.WriteLine("line3")

  # Closes the file
  myFile.Close()

VBScript

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

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

  ' Opens the created file for writing
  Set myFile = aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faWrite, aqFile.ctUnicode)

  ' Writes text to the file
  Call myFile.WriteLine("line1")
  Call myFile.WriteLine("line2")
  Call myFile.WriteLine("line3")

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

DelphiScript

procedure WriteTextToFile;
var sPath, myFile;
begin
  sPath := 'D:\Files\MyFile.txt';

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

  // Opens the created file for writing
  myFile := aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile.WriteLine('line1');
  myFile.WriteLine('line2');
  myFile.WriteLine('line3');

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

C++Script, C#Script

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

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

  // Opens the created file for writing
  var myFile = aqFileSystem["GetFileInfo"](sPath)["OpenTextFile"](aqFile.faWrite, aqFile.ctUnicode);

  // Writes text to the file
  myFile["WriteLine"]("line1");
  myFile["WriteLine"]("line2");
  myFile["WriteLine"]("line3");

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

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

JavaScript, JScript

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

  // Opens the specified file for reading
  var myFile = aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faRead, aqFile.ctUnicode);
  Log.Message("File by lines:");

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

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

Python

def ReadTextFromFile():
  sPath = "D:\Files\MyFile.txt"
  # Opens the specified file for reading 
  myFile = aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faRead, aqFile.ctUnicode)
  Log.Message("File by lines:")
  # Reads text lines from the file and posts them to the test log
  while not myFile.IsEndOfFile():
    s = myFile.ReadLine()
    Log.Message(s)
  # Closes the file 
  myFile.Close()

VBScript

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

  ' Opens the specified file for reading
  Set myFile = aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faRead, aqFile.ctUnicode)
  Log.Message("File by lines:")

  ' Reads text lines from the file and posts them to the test log
  While Not myFile.IsEndOfFile()
    s = myFile.ReadLine()
    Log.Message(s)
  WEnd

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

DelphiScript

procedure ReadTextFromFile;
var sPath, myFile;
begin
  sPath := 'D:\Files\MyFile.txt';

  // Opens the specified file for reading
  myFile := aqFileSystem.GetFileInfo(sPath).OpenTextFile(aqFile.faRead, aqFile.ctUnicode);
  Log.Message('File by lines:');

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

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

C++Script, C#Script

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

  // Opens the specified file for reading
  var myFile = aqFileSystem["GetFileInfo"](sPath)["OpenTextFile"](aqFile.faRead, aqFile.ctUnicode);
  Log["Message"]("File by lines:");

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

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

See Also

Working With Files From Scripts
aqTextFile Object
OpenBinaryFile Method
OpenTextFile Method

Highlight search results