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 following characters specifying a file mode, optionally followed by one of the characters listed in Table 2.

Returns

A file pointer to the file opened; null if the function fails.

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 follows:

Table 2.  File Mode Characters

Character

Mode

r

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.

Optional Characters

b

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

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

u

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

+

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 = 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 which opens a file and reads and writes a string, 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 which opens a file and reads and writes a string 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