fprintf - Maple Help

fprintf

prints expressions to a file or pipe based on a format string

sprintf

prints expressions to a string based on a format string

nprintf

prints expressions to a name based on a format string

printf

prints expressions to a default stream based on a format string

 Calling Sequence fprintf(file, fmt, x1, ..., xn) sprintf(fmt, x1, ..., xn) nprintf(fmt, x1, ..., xn) printf(fmt, x1, ..., xn)

Parameters

 file - file descriptor or filename fmt - output format specification x1, ..., xn - expressions to be formatted

Description

 • The fprintf command is based on a C standard library command of the same name. It uses the formatting specifications in the fmt string to format and write the expressions to the specified file.
 • The sprintf command is based on a C standard library command of the same name. It uses the formatting specifications in the fmt string to format and write the expressions into a Maple string, which is returned.
 • The nprintf command is the same as sprintf, except that it returns a Maple symbol (a simple name) instead of a string.
 • The printf command is based on a C standard library command of the same name. It uses the formatting specifications in the fmt string to format and display the expressions.
 • The call printf(...)  is equivalent to fprintf(default,...).
 • The fprintf command returns a count of the number of characters written.  Due to different character encodings, this number may differ from the number of bytes written to disk.
 • The format string, fmt, is a Maple symbol or string made up of a sequence of formatting specifications, possibly separated by other characters. Each formatting specification has the following format.

 %[flags][width][.precision][modifiers]code

 • The % symbol begins the format specification. One or more of the following flags can optionally follow the % symbol.

Flags

 +
 A signed numeric value is output with a leading positive or negative sign (+ or -), as appropriate.
 -
 The output is left justified instead of right justified.
 blank
 A signed numeric value is output with either a leading negative sign (-) or a leading blank, depending on whether the value is negative or non-negative.
 0
 The output is padded on the left (between the sign and the first digit) with zeroes. If - is also specified, 0 is ignored.
 {}
 The braces enclose a set of detailed formatting options for printing an rtable. These are described in more detail in the help page rtable_printf.
 • The optional width indicates the minimum number of characters to output for this field. If the formatted value has fewer characters, it is padded on the left (or right, if - was specified) with blanks.
 • The optional precision specifies the number of digits that appear after the decimal point for floating-point formats, or the maximum field width for string formats.
 • Both width and precision can be specified as *, in which case the value is taken from the argument list. The width and precision arguments must appear, in that order, before the argument that is being output. A negative width argument is equivalent to the - flag having appeared.

Modifiers

 The optional modifiers are used to indicate the type of the value to be printed:
 l or L
 The letters "l" and "L" are supported for compatibility with integer numeric formats as implemented by the C printf function (e.g. "%ld"), and indicate that a "long int" or "long long" is to be formatted. In Maple, these flags have no effect for numeric formats, but the L flag is used with the algebraic formats (e.g. "%a") to indicate that all remaining arguments should be formatted as a single expression sequence.
 zc or Z
 One of these flags can precede any of the numeric formats, namely d, o, x, e, f, or g, indicating that a complex value is to be formatted. Each of the real and imaginary parts of the complex value are formatted using the specified format, with the "z" or "Z" elided. The "z" format prints the real part, followed by a the character specified by c, followed by the imaginary part. The "Z" format prints the value in the form x+yi, where x is the real part, y is the imaginary part, and i is the current setting of interface(imaginaryunit). If y is negative, a "-" is output instead of a "+". If a supplied value is not complex, it is treated as a complex value with a zero imaginary part.

Formats

 • The code indicates the type the object is to be formatted as.  The code can be one of the following.

Integer Formats

 d
 The object is formatted as a signed decimal integer.
 o
 The object is formatted as an unsigned octal (base 8) integer.
 x or X
 The integer object is formatted as an unsigned hexadecimal (base 16) integer.  The digits corresponding to the decimal numbers 10 through 15 are represented by the letters "A" through "F" if uppercase X was specified, or "a" through "f" if lowercase x was specified.

Floating-Point Formats

 e or E
 The object is formatted as a floating-point number in scientific notation. One digit appears before the decimal point, and the number of digits specified by the precision appear after the decimal point (the default is six digits, if no precision is specified). This is followed by the letter e or E, and a signed integer specifying a power of 10. The power of 10 will have a sign and at least three digits, with leading zeroes added if necessary.
 If the value being formatted is infinity, -infinity, or undefined, the output is "Inf", "-Inf", or "NaN" respectively.
 f
 The object is formatted as a fixed point number. The number of digits specified by the precision appears after the decimal point (the six digits, if no precision is specified).
 If the value being formatted is infinity, -infinity, or undefined, the output is "Inf", "-Inf", or "NaN" respectively.
 g or G
 The object is formatted using e (or E if uppercase G was specified), or f format, depending on its value.  The precision determines the number of significant digits.  If not given, the precision defaults to 6.  A precision of 0 is treated as 1.  The object is formatted as e if the exponent in the printed form is less than -4 or greater than or equal to the precision, otherwise f formatting is used.  Trailing zeros are removed and a decimal point is only printed if followed by one or more digits.
 If the value being formatted is infinity, -infinity, or undefined, the output is "Inf", "-Inf", or "NaN" respectively.
 y or Y
 The floating-point object is formatted in byte-order-independent IEEE hex dump format (16 characters wide). At least 16 characters will always be output, regardless of the specified width. The precision is ignored. The digits corresponding to the decimal numbers 10 through 15 are represented by the letters "A" through "F" if uppercase Y was specified, or "a" through "f" if lowercase y was specified.

String Formats

 c
 The object, which must be a Maple symbol or string consisting of exactly one character, is output as a single character.
 s
 The object, which must be a Maple symbol, string, or one-dimensional Array with a datatype of integer[1], is output as a string of at least width characters (if specified) and at most precision characters (if specified).

Algebraic Formats

 a or A
 The object, which can be any Maple object, is output in correct Maple syntax. At least width characters are output (if specified), and at most precision characters are output (if specified). Note: Truncating a Maple expression by specifying a precision can result in an incomplete or incorrect Maple expression in the output.
 The "%a" and "%A" formats are identical, except that "%A" omits any quotes that normally appear around Maple symbols, and "%a" ignores the setting of interface(longdelim), always producing long style ending delimiters for Maple control structures (e.g. end if instead of fi) that require them.
 The following modifiers can be used with the "%a" and "%A" formats:
 – The L modifier makes "%a" and "%A" behave in the same way as "%q" and "%Q" respectively, consuming and printing all remaining arguments (see below).
 – The Z modifier causes line breaks, in the form of "\n" characters and whitespace for indentation, to be inserted into the output to reflect statement structure. Line breaking is performed instead of truncation, at precision characters.
 q or Q
 These are similar to "%a" and "%A", except that "%q" and "%Q" consume all remaining arguments and print them as an expression sequence, with each element formatted in "%a" or "%A" format respectively. No additional format specifiers can appear after "%q" or "%Q", since there will be no arguments left to format. The Z modifier can be used with "%q" and "%Q", as described for "%a" and "%A" above.
 m
 The object, which can be any Maple object, is output in the Maple .m file format. At least width characters are output (if specified), and at most precision characters are output (if specified).  Use the L modifier, "%Lm" as the last format specifier to output all remaining arguments as an expression sequence.  The Z modifier, "%Zm" can be used to generate an alternate equivalent ".m" representation that is used in communication with the GUI and in DocumentTools related functionality for the creation of XML content for .mw files. Note: Truncating a Maple ".m" format expression by specifying a precision can result in an incomplete or incorrect Maple expression in the output.
 v or V
 These are similar to "%A", except that the output is appropriately quoted and/or escaped to satisfy the requirements for "Comma Separated Values" (CSV) files. The "%v" format produces a field for a standard CSV file, and the "%V" format produces a field for a Microsoft CSV file. The differences between the two are in how quotes and other special characters are escaped within fields. The L modifier (e.g. "%Lv") can be used to format all remaining arguments as a single expression sequence.
 P
 This is similar to "%a", except that the output is in pretty-printed form (that is, the form printed by the print command in command-line Maple). The width, instead of specifying the length of output generated, is interpreted as a virtual screen width used for line wrapping. If no width is specified, the current setting of interface(screenwidth) is used. The output string will contain newline characters and spaces. The L modifier (e.g. "%LP") can be used to format all remaining arguments as a single expression sequence.

Printing a % symbol

 Use %% to output a verbatim percent symbol.

Other

 • Characters in fmt but not within a format specification are output verbatim.
 • All of the formats apply to Arrays (type Array), Matrices (type Matrix), and Vectors (type Vector), all of which are objects of type rtable.
 • If no rtable-specific formatting options are specified (via the {...} option, see rtable_printf), the %a, %A, %m, %v, and %V format codes print a representation of the rtable structure itself. For example, %a would print a Matrix, Vector, or Array call.
 • If no additional rtable-specific formatting options are specified for a format code other than %a, %A, %m, %v, and %V, or if an empty rtable option sequence (that is, just {}) is specified for any format code, the following default formatting is applied.
 - One-dimensional objects are formatted as one long line, with the elements separated by at least one space.
 - Objects of N dimensions, where N > 1, are formatted as a sequence of N-1-dimensional objects separated by N-2 blank lines. Therefore, two-dimensional objects are formatted in the obvious way, three-dimensional objects are formatted as a series of two-dimensional objects separated by blank lines, and so on.
 • Because Maple allows you to freely convert between types, several of the format specifications work with objects of types other than the ones they would be restricted to in a C program. Any of the floating-point formats can accept integer, rational, or floating-point objects; the object is evaluated to a floating-point value and output appropriately. Rationals are evaluated using the current setting of Digits, but output using the specified precision (or default precision, if none is specified).
 • The printf commands do not automatically start a new line at the end of the output when executed inside a procedure.  When invoked at the top-level in the worksheet interface, separate printf commands have new lines separating each command. If a new line is required, the fmt string must contain a newline character ("\n"). Also, output from the printf commands is not subject to line wrapping at interface(screenwidth) characters.
 • The %o, %x, and %X formats use the underlying implementation provided by the hardware vendor. As a result, output of octal and hexadecimal values is subject to the restrictions of the machine architecture.
 • Rounding may occur when printing floating-point numbers with the %e, %E, %f, %g, or %G format.   The rounding method specified by the Rounding environment variable is used.
 • If the file that fprintf is writing to is a STREAM or RAW file, and it is currently open in READ mode, it will be reopened in WRITE mode with type TEXT at the same offset.
 • If a filename is given to fprintf, and that file is not yet open, it is opened in WRITE mode with type TEXT.
 • For information on filenames, see file.

 • The printf, sprintf, nprintf and fprintf commands are thread safe as of Maple 15.
 • Parallel calls to file i/o commands on the same file descriptor will be serialized in an arbitrary order.  If you need the commands to execute in a particular order you must use Maple's synchronization tools to enforce this. See Threads:-Mutex.

Examples

 > $\mathrm{printf}\left("%g %g %g\n",123,\frac{123}{456},123456789\right)$
 123 0.269737 1.23457e+08
 > $\mathrm{printf}\left("%-2.5s:%2.5s:%2.5s\n",M,\mathrm{Map},\mathrm{MapleV}\right)$
 M :Map:Maple
 > $x≔23;$$y≔-\frac{1}{x}$
 ${x}{≔}{23}$
 ${y}{≔}{-}\frac{{1}}{{23}}$ (1)
 > $\mathrm{printf}\left("x=%+06.2f y=%+0*.*f y=%a y=%m\n",x,6,2,y,y,y\right)$
 x=+23.00 y=-00.04 y=-1/23 y=#!"""#B
 > $\mathrm{printf}\left("%a\n",\mathrm{sprintf}\left("%o %x\n\n",17129,17129\right)\right)$
 "41351 42e9\n\n"
 > $\mathrm{str}≔\mathrm{sprintf}\left("x = %d, y = %g",x,y\right)$
 ${\mathrm{str}}{≔}{"x = 23, y = -0.0434783"}$ (2)
 > $\mathrm{length}\left(\mathrm{str}\right)$
 ${22}$ (3)
 > $\mathrm{printf}\left("head=%a tail=%q\n",a,b,c,d,e,f\right)$
 head=a tail=b, c, d, e, f
 > $\mathrm{fd}≔\mathrm{fopen}\left("temp_file",\mathrm{WRITE}\right)$
 > $\mathrm{fprintf}\left(\mathrm{fd},"x = %d, y = %g",x,y\right)$
 > $\mathrm{fclose}\left(\mathrm{fd}\right)$