Clib Write Formatted String Method
The Clib Write Formatted String method writes a formatted string to a file.
Format
Clib.fprintf(filePointer, formatString)
The following table describes the arguments for the Clib Write Formatted String method.
Argument | Description |
---|---|
filePointer |
A file pointer that the Clib Open File method returns. |
formatString |
A string that contains formatting instructions for each data item that the Clib Write Formatted String method writes. For more information, Format Characters for Methods That Print and Scan. |
Example
The following example uses the Clib Write Formatted String method with various values for the formatString argument:
function Service_PreInvokeMethod (MethodName, Inputs, Outputs)
{
if (MethodName == "fprintfsamples")
{
var intgr = 123456789;
var flt = 12345.6789;
var hour = 1;
var min = 7;
var sec = 0;
var str = "Hello World";
var file = Clib.fopen("c:\\temp\\fprintf.txt", "w");
// Simple formatting:
Clib.fprintf(file, "(1) %s, it is now %i:%i:%i pm.\n", str, hour, min, sec);
Clib.fprintf(file, "(2) The number %i is the same as %x.\n", intgr, intgr);
Clib.fprintf(file, "(3) The result is %f.\n", flt);
// Flag values:
// "+" forces a + or - sign; "#" modifies the type flag "x"
// to prepend "0x" to the output. (Compare with the simple
// formatting example.)
Clib.fprintf(file, "(4) The number %+i is the same as %#x.\n", intgr, intgr);
// Width values:
// The width is a minimal width, thus longer values
// are not truncated.
// "2" fills with spaces, "02" fills with zeros.
var myWidth = 2;
Clib.fprintf(file, "(5) %5s, it is now %2i:%02i:%02i pm.\n", str, hour, min,
sec);
// Precision values:
// ".2" restricts to 2 decimals after the decimal separator.
// The number will be rounded appropriately.
Clib.fprintf(file, "(6) The result is %.2f.\n", flt);
// A combined example:
// <space> displays space or minus;
// "+" displays plus or minus;
// "020" uses a minimal width of 20, padded with zeros;
// ".2" displays 2 digits after the decimal separator;
// "*" uses the next argument in the list to specify the width.
Clib.fprintf(file, "(7) The values are:\n%+020.2f\n% 020.2f\n% *.2f", flt,
intgr, 20, intgr);
Clib.fclose(file);
return (CancelOperation);
}
return (ContinueOperation);
}
This example produces the following output:
(1) Hello World, it is now 1:7:0 pm.
(2) The number 123456789 is the same as 75bcd15.
(3) The result is 12345.678900.
(4) The number +123456789 is the same as 0x75bcd15.
(5) Hello World, it is now 1:07:00 pm.
(6) The result is 12345.68.
(7) The values are:
+0000000000012345.68
0000000123456789.00
123456789.00