ILE C/C++ Programmer's Guide


Using Library Functions with a Packed Decimal Data Type

You can use the va_arg macro to accept a packed decimal data of the form (n,p).You can write packed decimal constants to a file, and scan them back.

The following figure shows you how to use the va_arg macro to accept a packed decimal data of the form decimal(n,p). The va_arg macro returns the current packed decimal argument.

Figure 285. ILE C Source to Use the va_arg Macro with a Packed Decimal Data Type




/* This program uses the va_arg macro to accept a static decimal */
/* data type of the form decimal(n,p). The va_arg macro returns */
/* the current packed decimal argument. */
#include <decimal.h>
#include <stdio.h>
#include <stdarg.h>
#define N1 3
#define N2 6
int vargf(FILE *,char *,...);
int main(void)
{
int num1 = -1, num2 = -1;
char fmt_1[]="%D(3,2)%D(3,2)%D(3,2)";
char fmt_2[]="%D(3,2)%D(3,2)%D(3,2)%D(3,2)%D(3,2)%D(3,2)";
decimal(3,2) arr_1[]={ 1.11d, -2.22d, 3.33d };
decimal(3,2) arr_2[]={ -1.11d, 2.22d, -3.33d, 4.44d, -5.55d, 6.66d};
FILE *stream_1;
FILE *stream_2;
stream_1=fopen("file_1.dat", "wb+");
num1=vargf(stream_1,fmt_1,arr_1[0],
arr_1[1],
arr_1[2]);
if (num1<0)
{
printf("An error occurred when calling function vargf first time\n");
}
else
{
printf("Number of char. printed when vargf is called first time is:%d\n",
num1);
}
stream_2=fopen("file_2.dat", "wb+");

num2=vargf(stream_2,fmt_2,arr_2[0],
arr_2[1],
arr_2[2],
arr_2[3],
arr_2[4],
arr_2[5]);


if (num2<0)
{
printf("An error occurred when calling function vargf second time\n");
}
else
{
printf("Number of char. printed when vargf is called a second time is:%d\n",
num2);
}
fclose(stream_1);
fclose(stream_2);
}
int vargf(FILE *str, char *fmt,...)
{
int result;
va_list arg_ptr;
va_start(arg_ptr, fmt);
result = vfprintf(str, fmt, arg_ptr);
va_end(arg_ptr);
return result;
}

The output is as follows:

+--------------------------------------------------------------------------------+
|  Number of char. printed when vargf is called first time is: 13                |
|  Number of char. printed when vargf is called second time is : 27              |
|  Press ENTER to end terminal session.                                          |
+--------------------------------------------------------------------------------+

The following example shows you how to write packed decimal constants to a file, and how to scan them back. In addition, the example shows you how to pass a packed decimal array to a function.

Figure 286. ILE C Source to Write Packed Decimal Constants to a File and Scan Them Back




/* This program shows how to write packed decimal constants to a file */
/* and scan them back again. Also shows how to pass a packed decimal */
/* array to a function. */
#include <decimal.h>
#include <stdio.h>
#include <stdlib.h>
#define N 3 /* Array size */
/* for decimal declaration. */
FILE *stream; /* File pointer declaration. */
/* Declare valid packed decimal */
/* array. */
decimal(4,2) arr_1[] = {12.35d, 25.00d, -19.58d};
decimal(4,2) arr_2[N];
void write_num(decimal(4,2) a[N]); /*Declare function to */
/*write to a file. */
void read_num(decimal(4,2) b[N]); /*Declare function to */
/*read from a file. */



int main(void)
{
int reposition=0;
/* Open the file. */
if ((stream = fopen("*CURLIB/OUTFILE","w+")) == NULL)
{
printf("Can not open file");
exit(EXIT_FAILURE);
}
write_num(arr_1); /* Call function to write */
/* values of packed decimal */
/* array to outfile with fprintf*/
/* library function. */
reposition=fseek(stream, 0L, SEEK_SET);
if (reposition!=0)
{
printf("FSEEK failed to position file pointer\n");
exit(EXIT_FAILURE);
}
read_num(arr_2); /* Call function to read */
/* values of packed decimal */
/* array from file using */
/* fscanf() function. */
fclose(stream); /* Close the file. */
}
/* write_num is passed a packed decimal array. These values are */
/* written to a text file with the fprintf library function. */
/* If the function is successful a 0 is returned, otherwise a */
/* negative value is returned (indicating an error). */

void write_num(decimal(4,2) a[N])
{
int i, j;

for (i=0;i < N;i++)
{
j = fprintf(stream,"%D(4,2)\n",a[i]);
if (j < 0)
printf("Number not written to file %D(4,2)\n",a[i]);
}
}
/* read_num is passed a packed decimal array. The values are */
/* read from a text file with the fscanf library function. */
/* If the function is successful a 0 is returned, otherwise a */
/* negative value is returned (indicating an error). */
void read_num(decimal(4,2) b[N])
{
int i, j;
for (i=0;i < sizeof(b)/sizeof(b[0]);i++)
{
j = fscanf(stream,"%D(4,2)\n",&b[i]);
if (j < 0)
printf("Error when reading from file\n");
}
printf("b[0]=%D(4,2)\nb[1]=%D(4,2)\n\
b[2]=%D(4,2)\n", b[0], b[1], b[2]);
}

The output is as follows:

+--------------------------------------------------------------------------------+
|  b[0]=12.35                                                                    |
|  b[1]=25.00                                                                    |
|  b[2]=-19.58                                                                   |
|  Press ENTER to end terminal session.                                          |
+--------------------------------------------------------------------------------+

The following example shows how to use the %D(*,*) specifier with the printf() function. If n and p of the variable to be printed do not match with the n and p in the conversion specifier %D(n,p), the behavior is undefined. Use the unary operators digitsof (expression) and precisionof (expression) in the argument list to replace the * in D(*,*) whenever the size of the resulting type of a packed decimal expression is not known.

Figure 287. ILE C Source to Print Packed Decimal Constants




#include <decimal.h>
#include <stdio.h>
int main(void)
{
decimal(6,2) op_1=1234.12d;
decimal(10,2) op_2=-12345678.12d;
printf("op_1 = %*.*D(*,*)\n", 6, 2, digitsof(op_1),
precisionof(op_1), op_1);
printf("op_2 = %*.*D(*,*)\n", 10, 2, digitsof(op_2),
precisionof(op_2), op_2);
}

The output is as follows:

+--------------------------------------------------------------------------------+
|  op_1 = 1234.12                                                                |
|  op_2 = -12345678.12                                                           |
|  Press ENTER to end terminal session.                                          |
+--------------------------------------------------------------------------------+


[ Top of Page | Previous Page | Next Page | Table of Contents ]