Siebel eScript Language Reference > Siebel eScript Commands > File I/O Methods in eScript >

Clib.fopen() Method


This method opens a specified file in a specified mode.

Syntax

Clib.fopen(filename, mode)

Parameter
Description

filename

Any valid filename that does not include wildcard characters

mode

One of the required characters specifying a file mode, followed by optional characters, as described in Table 33

Returns

This method returns a file pointer to the file opened or null, if the function fails. This return value is on object of type File.

NOTE:  Several Clib methods require an argument denoted as filePointer in this document. These input arguments are of type File and are often the return value of a Clib.fopen() call.

Usage

This function opens the file filename, in mode mode. The mode parameter is a string composed of "r", "w", or "a" followed by other characters as shown in Table 33.

Table 33. Clib.fopen() Mode Parameters

Parameter

Required?

Mode

r

Yes, only one of these parameters is required.

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

w

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

a

Opens the file in append mode.

b

No

Opens the file in binary mode; if b is not specified, the file is opened in text mode (end-of-line translation is performed)

t

No

Opens the file in text mode. Do not use for non-ASCII characters, use "u" instead.

u

No

Opens the file in Unicode mode; for example, Clib.fopen("filename.txt", "rwu"). Use this mode for both ASCII and non-ASCII characters.

+

No

Opens the file for update (reading and writing).

When a file is successfully opened, its error status is cleared and a buffer is initialized for automatic buffering of reads and writes to the file.

Example

The following code fragment opens the text file ReadMe 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);

Here is an example that opens a file, writes a string to the file, then reads the string from the file, using the default codepage:

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);
}

Here is an example that opens a file, writes a string to the file, then reads the string from the file, in Unicode mode:

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

See Also

Clib.getenv() Method
Clib.tmpfile() Method

Siebel eScript Language Reference Copyright © 2007, Oracle. All rights reserved.