Clib Open File Method

The Clib Open File method opens the file that you specify in the filename argument. It opens it in the mode that you specify in the mode argument. It returns one of the following values:

  • If successful, then it returns a file pointer to the file that it opened.

  • If not successful, then it returns the following value:

    Null

If this method successfully opens a file, then it clears the error status for this file and initializes a buffer for automatic buffering of read and write activity with the file.

Several Clib methods require an argument named filePointer. It is often the return value of a Clib Open File call.

Format

Clib.fopen(filename, mode, bUseBOM)

The following table describes the arguments for the Clib Open File method.

Argument Description

filename

Any valid file name that does not include a wildcard character.

mode

One of the required characters that specify a file mode followed by optional characters. For more information, see Clib Open File Method.

bUseBOM

true: Add the Byte Order Mark (BOM) to the file.

false: Don't add the Byte Order Mark (BOM) to the file.

Usage for the Mode Argument

The following table describes usage for the mode argument. The mode argument is a string that includes one of the following required characters, and then followed by other optional characters:

  • r

  • w

  • a

Argument

Mode

Required

r

Opens the file for reading. The file must already exist.

Yes. You must include one of these arguments.

w

Opens the file for writing. If the file does not exist, then Siebel eScript creates the file.

a

Opens the file in append mode.

b

Opens the file in binary mode. If you do not specify b, then this method opens the file in text mode and performs an end-of-line translation.

No

t

Opens the file in text mode. For a non-ASCII character:

  • You use the u argument.

  • You do not use the t argument.

No

u

Opens the file in Unicode mode as UTF-16 or Little Endian. For example:

Clib.fopen(“filename.txt”, “rwu”)

You can use the u mode for ASCII and non-ASCII characters.

No

+

Opens the file for reading and writing.

No

Because some systems expect text files without the Byte Order Mark (BOM) included in the file, a new parameter will prevent the inclusion of a BOM in a file. Previously all text files written to from Siebel eScript included the BOM in the file as a default.

Clib.fopen(filename, mode, bUseBOM)

Default value of bUseBOM is true which will, when writing the file, write the byte order mark to the file.

If a byte order mark is not required in the file, then the third parameter should have the value “false”.

For example, var oFile = Clib.fopen("c:\\temp\\firstfile.txt","w", false);

Example 1

The following example opens the ReadMe text file for text mode reading and displays each line in that file:

var fp:File = Clib.fopen("ReadMe","rt");
if ( fp == null ) 
   TheApplication().RaiseErrorText("\aError opening file for reading.\n")
else 
{
   while ( null != (line=Clib.fgets(fp)) )
   {
      Clib.fputs(line, stdout)
   }
}
Clib.fclose(fp);

The following example opens a file, writes a string to that file, and then uses the default codepage to read the string from this file:

var oFile = Clib.fopen("myfile","rw");
if (null != oFile)
{
   var sHello = "Hello";
   var nLen = sHello.length;
   Clib.fputs(sHello, oFile);
   Clib.rewind(oFile);
   Clib.fgets (nLen, sHello);
}

The following example opens a file, writes a string to this file, then uses Unicode to read the string from this file:

var oFile = Clib.fopen("myfile","rwu");
if (null != oFile)
{
   var sHello = "Hello";
   var nLen = sHello.length;
   Clib.fputs(sHello, oFile);
   Clib.rewind(oFile);
   Clib.fgets (nLen, sHello);
}

The following example specifies a file path:

function WebApplet_ShowControl (ControlName, Property, Mode, &HTML)
{
if (ControlName == "GotoUrl")
   {
      var fp = Clib.fopen("c:\\test.txt","wt+");
      Clib.fputs("property = " + Property + "\n", fp);
      Clib.fputs("mode = " + Mode + "\n",fp);
      Clib.fputs("ORG HTML = " + HTML + "\n",fp);
      Clib.fclose(fp);
      HTML = "<td>New HTML code</td>";
   }
return(ContinueOperation);

For more information, see the following topics: