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: