使用具有压缩十进制数据类型的库函数

您可以使用 va_arg 宏接受格式为n , p的压缩十进制数据。 您可以将压缩十进制常量写入文件,然后重新扫描这些常量。

下图显示如何使用 va_arg 宏接受格式为 decimal (np)的压缩十进制数据。 va_arg 宏返回当前压缩十进制参数。

/* 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;
    }
输出如下所示:
  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.
以下示例显示如何将压缩十进制常量写入文件,以及如何将其扫描回。 此外,该示例还显示了如何将压缩十进制数组传递到函数。
/* 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]);
}
输出如下所示:
  b[0]=12.35
  b[1]=25.00
  b[2]=-19.58
  Press ENTER to end terminal session.
以下示例显示如何将 %D (* , *) 说明符与 printf() 函数配合使用。 如果要打印的变量的 n 和 p 与转换说明符 %D (n , p) 中的 n 和 p 不匹配,那么行为未定义。 在自变量列表中使用一元运算符 digitsof (expression) 和 precisionof (expression) 来替换 D (* , *) 中的 * ,只要生成的压缩十进制表达式类型的大小未知。
图 1。 ILE C 源到打印压缩十进制常量
#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);
}
输出如下所示:
  op_1 = 1234.12
  op_2 = -12345678.12
  Press ENTER to end terminal session.