# Fmt/FmtFile/FmtOut Examples

This section contains examples of program code that use Fmt, FmtFile, and FmtOut from the Formatting and I/O Library. To eliminate redundancy, error checking on I/O operations has been omitted from all the examples in this section except the Two Integers to ASCII File with Error Checking example.

The Fmt/FmtFile/FmtOut examples are logically organized as shown:

Integer to string
Short integer to string
Real to string in floating-point notation
Real to string in scientific notation
Integer and real to string with literals
Two integers to ASCII file with error checking
Real array to ASCII file in columns and with comma separators
Integer array to binary file, assuming a fixed number of elements
Real array to binary file, assuming a fixed number of elements
Real array to binary file, assuming a variable number of elements
Variable portion of a real array to a binary file
Concatenating two strings
Appending to a string
Creating an array of filenames
Writing a line that contains an integer with literals to the standard output
Writing to the standard output without a linefeed or carriage return

## Integer to String

char buf[10];
int a;
a = 16;
Fmt (buf, "%s<%i", a); /* result: "16" */
a = 16;
Fmt (buf, "%s<%x", a); /* result: "10" */
a = 16;
Fmt (buf, "%s<%o", a); /* result: "20" */
a = -1;
Fmt (buf, "%s<%i", a); /* result: "-1" */
a = -1;
Fmt (buf, "%s<%i[u]", a); /* result: "4294967295" */
a = 1234;
Fmt (buf, "%s<%i[w6]", a); /* result: " 1234" */
a = 1234;
Fmt (buf, "%s<%i[w6p0]", a); /* result: "001234" */
a = 1234;
Fmt (buf, "%s<%i[w2]", a); /* result: "*4" */

The results shown are the contents of buf after each call to Fmt. The last call demonstrates what occurs when the w modifier specifies a width that is too small.

## Short Integer to String

char buf[20];
short a;
a = 12345;
Fmt (buf, "%s<%i[b2]", a); /* result: "12345" */
a = -1;
Fmt (buf, "%s<%i[b2]", a); /* result: "-1" */
a = -1;
Fmt (buf, "%s<%i[b2u]", a); /* result: "65535" */
a = 12345;
Fmt (buf, "%s<%i[b2w7]", a); /* result: " 12345" */
a = 12345;
Fmt (buf, "%s<%i[b2w7p0]", a); /* result: "0012345" */
a = 12345;
Fmt (buf, "%s<%i[b2w4]", a); /* result: "*345" */

The results shown are the contents of buf after each call to Fmt. The last call demonstrates what occurs when the w modifier specifies a width that is too small.

## Real to String in Floating-Point Notation

char buf[30];
double x;
x = 12.3456789;
Fmt (buf, "%s<%f", x); /* result: "12.345679" */
x = 12.3456789;
Fmt (buf, "%s<%f[p2]", x); /* result: "12.35" */
x = 12.3456789;
Fmt (buf, "%s<%f[p10]", x); /* result: "12.3456789000" */
x = 12.345;
Fmt (buf, "%s<%f", x); /* result: "12.345" */
x = 12.345;
Fmt (buf, "%s<%f[p0]", x); /* result: "12" */
x = 12.345;
Fmt (buf, "%s<%f[p6]", x); /* result: "12.345000" */
x = -12.345;
Fmt (buf, "%s<%f[w12]", x); /* result: " -12.345" */
x = -12.3456789;
Fmt (buf, "%s<%f[w6]", x); /* result: "-12.3*" */
x = 0.00000012;
Fmt (buf, "%s<%f[p8]", x); /* result: "0.00000012" */
x = 0.00000012;
Fmt (buf, "%s<%f", x); /* result: "1.2e-007" */
x = 4.5e050;
Fmt (buf, "%s<%f", x); /* result: "4.5e050" */

The results shown are the contents of buf after each call to Fmt. The last two calls demonstrate that Fmt sometimes forces very large and very small values into scientific notation even when the e modifier is absent.

## Real to String in Scientific Notation

char buf[20];
double x;
x = 12.3456789;
Fmt (buf, "%s<%f[e]", x); /* result: "1.234568e+001" */
x = 12.3456789;
Fmt (buf, "%s<%f[ep2]", x); /* result: "1.23e+001" */
x = 12.3456789;
Fmt (buf, "%s<%f[e2p2]", x); /* result: "1.23e+01" */
x = 12.345;
Fmt (buf, "%s<%f[e]", x); /* result: "1.2345e+001" */
x = 12.345;
Fmt (buf, "%s<%f[ep2w12]", x); /* result: " 1.23e+001" */
x = 12.345;
Fmt (buf, "%s<%f[ep2w6]", x); /* result: "1.23e*" */

The results shown are the contents of buf after each call to Fmt. The last call demonstrates what occurs when the w modifier specifies a width that is too small.

## Integer and Real to String with Literals

char buf[20];
int f, r;
double v;
f = 4;
r = 3;
v = 1.2;
Fmt (buf, "%s<F%iR%i; V%f;", f, r, v);

After the Fmt call, buf contains "F4R3; V1.2;".

## Two Integers to ASCII File with Error Checking

int a, b, n, file_handle;
a = 12;
b = 456;
file_handle = OpenFile ("FILE.DAT", 2, 0, 1);
if (file_handle < 0) {

FmtOut ("Error opening file\n");
exit (1);

}
n = FmtFile (file_handle, "%s<%i %i", a, b);
if (n != 2) {

FmtOut ("Error writing file\n");
exit (1);

}
CloseFile (file_handle);

OpenFile opens FILE.DAT as an ASCII file for writing only. If OpenFile succeeds, it returns a file handle with a positive integer value. FmtFile writes the ASCII representation of two integer values to the file. If FmtFile succeeds, it returns 2 because the format string contains two source specifiers.

## Real Array to ASCII File in Columns and with Comma Separators

double x[100];
int file_handle, i;
file_handle = OpenFile ("FILE.DAT", 2, 0, 1);
for (i=0; i < 100; i++) {

FmtFile (file_handle, "%s<%f[w15],", x[i]);
if ((i % 5) == 4)

WriteFile (file_handle, "\n", 1);

}
CloseFile (file_handle);

FmtFile writes the ASCII representation of a real array element to the file, followed by a comma. The w modifier specifies that the number be right-justified in a 15-character field. The WriteFile call writes a linefeed to the file after every fifth call to FmtFile. Because the file is opened in ASCII mode, FmtFile automatically writes the linefeed as a linefeed/carriage return combination.

| Note If the format string is "%s[w15]<%f,", FmtFile left-justifies the number and the comma together in a 15-character field.

## Integer Array to Binary File, Assuming a Fixed Number of Elements

int readings[100];
int file_handle, nbytes;
file_handle = OpenFile ("FILE.DAT", 2, 0, 0);
FmtFile (file_handle, "%100i<%100i", readings);
nbytes = NumFmtdBytes ();
CloseFile (file_handle);

FmtFile writes all 100 elements of the readings integer array to a file in binary form. If the FmtFile call succeeds, nbytes = 200 (100 integers, 2 bytes per integer).

## Real Array to Binary File, Assuming a Fixed Number of Elements

double waveform[100];
int file_handle, nbytes;
file_handle = OpenFile ("FILE.DAT", 2, 0, 0);
FmtFile (file_handle, "%100f<%100f", waveform);
nbytes = NumFmtdBytes ();
CloseFile (file_handle);

FmtFile writes all 100 elements of the waveform real array to a file in binary form. If the FmtFile call succeeds, nbytes = 800 (100 integers, 8 bytes per real number).

## Real Array to Binary File, Assuming a Variable Number of Elements

void StoreArray (double x[], int count, char filename[])
{

int file_handle;
file_handle = OpenFile (filename, 2, 0, 0);
FmtFile (file_handle, "%*f<%*f", count, count, x);
CloseFile (file_handle);

}

This example shows how you can use a function to write an array of real numbers to a binary file. The function parameters are a real array, the number of elements to be written, and the filename.

FmtFile writes the first count elements of x to a file in binary form. FmtFile matches the two asterisks (*) in the format string to count. For instance, if count is 100, the format string is equivalent to %100f<%100f.

## Variable Portion of a Real Array to a Binary File

void StoreSubArray (double x[], int start, int count, char filename[])
{

int file_handle;
file_handle = OpenFile (filename, 2, 0, 0);
FmtFile (file_handle, "%*f<%*f[i*]", count, count, start, x);
CloseFile (file_handle);

}

This example is an extension of the previous example. The function writes a variable number of elements of a real array to a file. Instead of beginning at the first element of the array, you pass a starting index to the function.

FmtFile writes count elements of x, starting from x[start], to a file in binary form. FmtFile matches the first two asterisks (*) in the format string to count. FmtFile matches the third asterisk to start. For instance, if count is 100 and start is 30, the format string is equivalent to %100f<%100f[i30]. Because the i modifier specifies a zero-based index into the real array, FmtFile writes the array elements from x[30] through x[129] to the file.

## Concatenating Two Strings

char buf[30];
int wave_type, signal_output;
char *wave_str, *signal_str;
int nbytes;
wave_type = 1;
signal_output = 0;
switch (wave_type) {

case 0:

wave_str = "SINE;";
break;

case 1:

wave_str = "SQUARE;";
break;

case 2:

wave_str = "TRIANGLE;";
break;

}
switch (signal_output) {

case 0:

signal_str = "OUTPUT OFF;";
break;

case 1:

signal_str = "OUTPUT ON;";
break;

}
Fmt (buf, "%s<%s%s", wave_str, signal_str);
nbytes = NumFmtdBytes ();

The two switch constructs assign constant strings to the string variables wave_str and signal_str. The Fmt call concatenates the contents of wave_str and signal_str into buf. After the call, buf contains "SQUARE;OUTPUT OFF;". NumFmtdBytes returns the number of bytes in the concatenated string.

## Appending to a String

char buf[30];
int wave_type, signal_output;
int nbytes;
switch (wave_type) {

case 0:

Fmt (buf, "%s<SINE;");
break;

case 1:

Fmt (buf, "%s<SQUARE;");
break;

case 2:

Fmt (buf, "%s<TRIANGLE;");
break;

}
switch (signal_output) {

case 0:

Fmt (buf, "%s[a]<OUTPUT OFF;");
break;

case 1:

Fmt (buf, "%s[a]<OUTPUT ON;");
break;

}
nbytes = StringLength (buf);

This example shows how to append characters to a string without writing over the existing contents of the string. The first switch construct writes one of three strings into buf. The second switch construct appends one of two strings to the string already in buf. After the call, buf contains "SQUARE;OUTPUT OFF;". Notice that the a modifier applies to the target specifier.

StringLength returns the number of bytes in the resulting string. In this case, Fmt uses StringLength instead of NumFmtdBytes, because NumFmtdBytes returns only the number of bytes appended.

## Creating an Array of Filenames

char *fname_array[4];
int i;
for (i=0; i < 4; i++){

fname_array[i] = malloc(14);
Fmt (fname_array[i], "%s<FILE%i[w4p0].DAT", i);

}

To allocate the space for each filename in the array, you must assign a separate constant string to each array element. Use Fmt to format each filename. The resulting filenames are FILE0000.DAT, FILE0001.DAT, FILE0002.DAT, and FILE0003.DAT.

## Writing a Line That Contains an Integer with Literals to the Standard Output

int a, b;
a = 12;
b = 34;
FmtOut ("%s<A = %i\n", a);
FmtOut ("%s<B = %i\n", b);

In this example, the output is as follows:

A = 12
B = 34

## Writing to the Standard Output without a Linefeed or Carriage Return

char *a;
int b;
double c;
a = "One";
FmtOut ("%s<%s ", a);
b = 2;
FmtOut ("%s<%i ", b);
c = 3.4;
FmtOut ("%s<%f", c);

This example demonstrates how to write to the Standard Output without a linefeed or carriage return by omitting the '\n' from the format string. The output in this example is as follows:

One 2 3.4

The following code produces the same output:

a = "One";
b = 2;
c = 3.4;
FmtOut ("%s<%s %i %f", a, b, c);