Siebel eScript Language Reference > C Language Library Reference > Clib File Input and Output Methods >

Format Characters for Methods That Print and Scan


A method that prints or scans uses a format string to format the data that the method reads and writes.

Format Characters for Methods That Print

This topic describes format characters for methods that print. The following methods can perform print operations:

Each of these methods prints each character while it reads the input until the method encounters a percentage symbol (%). This symbol instructs that method to use the following format to print a value:

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

To include the % symbol as a character in the string, you use two consecutive percentage symbols (%%).

Characters That Format Values

Table 134 describes characters that format a value.

Table 134. Characters That Format a Value
Character
Description
Example Statement and Output

-

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 symbol (+) or a minus symbol (-).

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

space

A negative value that begins with a minus symbol (-). A positive value begins with a space.

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

#

Append one of the following symbols to the pound (#) character to display the output in one of the following forms:

  • o. Prefix a zero to nonzero octal output.
  • x or X. Prefix 0x or 0X to the output, which indicates hexadecimal.
  • f. Include a decimal point even if no digits follow the decimal point.
  • e or E. Include a decimal point even if no digits follow the decimal point, and display the output in scientific notation.
  • g or G. 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

f

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

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

e

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

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

E

Floating-point of the format [-]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, or 8.

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

s

String.

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

Characters That Determine Width

Table 135 describes characters that determine width.

Table 135. Characters That Determine Width
Character
Description
Example Statement and Output

n

At least n characters are output. If the value is less than n characters, then Siebel eScript pads the output 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 argument list is an integer that specifies the output width.

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

Characters That Determine Precision

Table 136 describes characters that determine precision. If you specify precision, then you must begin the precision format with a period (.) and you must use one of the forms described in Table 134.

Table 136. Characters That Determine Precision
Character
Description
Example Statement and Output

.0

For floating-point type. No decimal point is output.

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

.n

Output is n characters. If the value is a floating-point number, then the output is n decimal places.

Assume you specify a Width value and a .n Precision value when you format a floating point number. In this situation, to determine the width of the output and to determine if it must pad the output, the method counts the decimal point and the characters that occur before and after the decimal point. For example:

fprintf(file, "%10.2f", 26.735);
[     26.73]

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

.*

The next value in the argument list is an integer that specifies the precision width.

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

Characters That Determine Character Type

Table 137 describes characters that determine character type.

Table 137. Characters That Determine Character Type
Character
Description
Example Statement and Output

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, or f.

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

X

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

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

f

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

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

e

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

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

E

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

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

g

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

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

G

Floating-point number of F or E, 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

Format Characters for Methods That Scan

This topic describes format characters for methods that scan. The following methods can perform a scan operation:

Note the following:

  • The format string includes character combinations that specify the type of data.
  • The format string specifies input sequences and how the method must convert the input.
  • The method maps each character to the input as it reads the input until it encounters a percentage symbol (%).
  • The percentage symbol causes the method to read the value, and then store it in an argument that follows the format string.
  • Each argument that occurs after the format string receives the next parsed value from the next argument in the list of arguments that occur after the format string.
Arguments In a Method That Performs a Scan Operation

An argument in a method that performs a scan operation uses the following format:

%[*][width]type

Table 138 describes usage of the * (asterisk) and the width argument. If you specify the width, then the input is an array of characters of the length that you specify.

Table 138. Usage of the Asterisk and Width Arguments in a Method That Performs a Scan Operation
Argument
Description

*

Suppresses assigning this value to any argument.

width

Sets the maximum number of characters to read. If the method encounters a white-space character or a nonconvertible character, then it stops reading these characters. For more information, see Use White Space to Improve Readability.

Table 139 describes the values you can use for the type argument.

Table 139. Usage of the Type Argument in a Method That Performs a Scan Operation
Type Value
Description

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 that includes the characters in brackets, where A-Z represents the range A to Z.

[^abc]

String that includes the following character in brackets:

not

Example

The following example creates a file named myfile.txt and stores a float number and a string. It then rewinds the stream and uses fscanf to read the values:

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

This example produces the following output:

I have read: 3.141600 and PI

Siebel eScript Language Reference Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices.