Siebel eScript Language Reference > Siebel eScript Commands >

Formatting Data in eScript


The print functions and scan functions both use format strings to format the data written and read, respectively.

Formatting Output in eScript

Table 33 lists the format strings for use with the print functions: fprintf() (see Clib.fprintf() Method), rsprintf(), and sprintf() (see Clib.rsprintf() Method). In these functions, characters are printed as read to standard output until a percent character (%) is reached. The percent symbol (%) indicates that a value is to be printed from the parameters following the format string. The form of the format string is as follows:

%[flags][width][.precision]type

To include the % character as a character in the format string, use two percent characters together (%%).

Table 33. Format Strings for the Print Functions
Formatting Character
Effect
Example statement and output

Flag Values

 

 

-

Left justification in the field with space padding or right justification with zero or space padding

fprintf(file, "[%-8i]", 26);
[26 ]

+

Force numbers to begin with a plus (+) or minus (-)

fprintf(file, "%+i", 26);
+26

space

Negative values begin with a minus (-); positive values begin with a space

fprintf(file, "[% i]", 26);
[ 26]

#

Append one of the following symbols to the # character to display the output in the indicated form:

  • o to prefix a zero to nonzero octal output
  • x or X to prefix 0x or 0X to the output, signifying hexadecimal
  • f to include a decimal point even if no digits follow the decimal point
  • e or E to include a decimal point even if no digits follow the decimal point, and display the output in scientific notation
  • g or G to include a decimal point even if no digits follow the decimal point, display the output in scientific notation (depending on precision), and leave trailing zeros in place

fprintf(file, "%#o", 26);
032

fprintf(file, "%#x", 26);
0x1A

fprintf(file, "%#.f", 26);
26.

fprintf(file, "%#e", 26);
2.600000e+001

fprintf(file, "%#g", 26);
26.0000

Width Values

 

 

n

At least n characters are output; if the value is fewer than n characters, the output is padded on the left with spaces.

fprintf(file, "[%8s]", "Test");
[ Test]

0n

At least n characters are output, padded on the left with zeros.

fprintf(file, "%08i", 26);
00000026

*

The next value in the parameter list is an integer specifying the output width.

fprintf(file, "[%*s]", 8, "Test");
[ Test]

Precision Values

 

 

If precision is specified, then it must begin with a period (.) and must take one of the following forms:

 

.0

For floating-point type, no decimal point is output.

fprintf(file, "%.0f", 26.735);
26

.n

Output is n characters, or n decimal places if the value is a floating-point number.

fprintf(file, "%.2f", 26.735);
26.73

.*

The next value in the parameter list is an integer specifying the precision width.

fprintf(file, "%.*f", 1, 26.735);
26.7

Type Values

 

 

d,i

Signed integer

fprintf(file, "%i", 26);
26

u

Unsigned integer

fprintf(file, "%u", -1);
4294967295

o

Octal integer

fprintf(file, "%o", 26);
32

x

Hexadecimal integer using 0 through 9 and a, b, c, d, e, f

fprintf(file, "%x", 26);
1a

X

Hexadecimal integer using 0 through 9 and A, B, C, D, E, F

fprintf(file, "%X", 26);
1A

f

Floating-point of the form [-]dddd.dddd

fprintf(file, "%f", 26.735);
26.735000

e

Floating-point of the form [-]d.ddde+dd or [-]d.ddde-dd

fprintf(file, "%e", 26.735);
2.673500e+001

E

Floating-point of the form [-]d.dddE+dd or [-]d.dddE-dd

fprintf(file, "%E", 26.735);
2.673500E+001

g

Floating-point number of f or e type, depending on precision

fprintf(file, "%g", 26.735);
26.735

G

Floating-point number of F or E type, depending on precision

fprintf(file, "%G", 26.735);
26.735

c

Character; for example, 'a', 'b', '8'

fprintf(file, "%c", 'a');
a

s

String

fprintf(file, "%s", "Test");
Test

Formatting Input in eScript

Format strings are also used with the scan functions: fscanf() (see Clib.fscanf() Method), sscanf() (see Clib.sscanf() Method), and vfscanf(). The format string contains character combinations that specify the type of data expected. The format string specifies the admissible input sequences and how the input is to be converted to be assigned to the variable number of parameters passed to the function. Characters are matched against the input as read and as it matches a portion of the format string until a percent character (%) is reached. The percent character indicates that a value is to be read and stored to subsequent parameters following the format string.

Each subsequent parameter after the format string gets the next parsed value taken from the next parameter in the list following the format string. A parameter specification takes this form:

%[*][width]type

The * and width values may be as shown on Table 34.

Table 34. Scan Functions Formatting Parameters * and width
Parameter
Description

*

Suppresses assigning this value to any parameter.

width

Sets the maximum number of characters to read. Fewer are read if a white-space or nonconvertible character is encountered.

If width is specified, the input is an array of characters of the specified length.

Table 35 lists the characters that define the type.

Table 35. Type Values for the Scan Functions
Type Value
Effect

d,D,i,I

Signed integer

u,U

Unsigned integer

o,O

Octal integer

x,X

Hexadecimal integer

f,e,E,g,G

Floating-point number

s

String

[abc]

String consisting of the characters within brackets, where A-Z represents the range A to Z

[^abc]

String consisting of the character not within brackets

Example

This sample script creates a file called myfile.txt and stores a float number and a string. Then the stream is rewound and both values are read with fscanf.

function WebApplet_Load()
{
   var f;
   var str;
   var pFile = Clib.fopen ("c:\\myfile.txt","w+");
   Clib.fprintf (pFile, "%f %s", 3.1416, "PI");
   Clib.rewind (pFile);
   Clib.fscanf (pFile, "%f", f);
   Clib.fscanf (pFile, "%s", str);
   Clib.fclose (pFile);
   Clib.printf ("I have read: %f and %s \n",f,str);
}

Here are the trace results from the script:

I have read: 3.141600 and PI

Siebel eScript Language Reference