使用 _DecimalT 类模板的文件 I/O

可以使用 C 运行时文件 I/O 命令 (fopen , fprintf 等) 将 _DecimalT 类模板对象写入文件并从文件中读取。 或 C++ ofstream 对象。

下图提供了一个示例。

// This program shows how to write _DecimalT class template
// constants to a file
// and scan them back again. Shows how to pass a _DecimalT
// class template array to a function.

#include <bcd.h>
#include <iostream.h>
#include <stdlib.h>

#define N    3          // Array size for decimal declaration.

FILE *stream;                        // File pointer declaration.

                                     // Declare valid array.

_DecimalT<4,2> arr_1[] = {__D("12.35"), __D("25.00"),
                                              __D("-19.58")};
_DecimalT<4,2> arr_2[N];

void write_num(_DecimalT<4,2> a[N]); //Declare function to
                                      // write to a file.

void read_num(_DecimalT<4,2> b[N]); //Declare function to
                                       //read from a file.

int main(void)
{
int reposition=0;
                                    // Open the file. Must use fopen()
                                            // to access a physical file.
if ((stream = fopen("*CURLIB/OUTFILE","w+")) == NULL)
{
   cout <<"Can not open file" << endl;
   exit(EXIT_FAILURE);
}
write_num(arr_1);     // Call function to write values of the
                        // array to outfile with fprintf().

reposition=fseek(stream, 0L, SEEK_SET);

if (reposition!=0)
  {
    cout <<"FSEEK failed to position file pointer" <<endl;
    exit(EXIT_FAILURE);

  }
read_num(arr_2);      // Call function to read values of the
                         // array from file using fscanf().

fclose(stream);       // Close the file.
}
// write_num is passed a the array.  These values are written to a
// text file with fprintf(). If the function is successful a 0 is
// returned, otherwise a negative value is returned (indicating an
// error.

void write_num(_DecimalT<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)
        cout <<"Number not written to file" <<a[i] <<endl;
      }
}
// read_num is passed a the array.  The values are
// read from a text file with fscanf().
// If the function is successful a 0 is returned, otherwise a
// negative value is returned (indicating an error).

void read_num(_DecimalT<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)
       cout <<"Error when reading from file" <<endl;
     }
   cout <<"b[0]=" <<b[0] <<endl;
   cout <<"b[1]=" <<b[1] <<endl;
   cout <<"b[2]=" <<b[2] <<endl;
}
输出为:
b[0]=12.35
b[1]=25.00
b[2]=-19.58

您可以重写此程序以使用 ofstream 类,如下图所示:

// This program shows how to write _DecimalT template
// constants to a file
// and scan them back again. Shows how to pass a _DecimalT
// class template array to a function.

#include <bcd.h>
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

#define N    3                       // Array size
                                       // for decimal declaration.

                                     // Declare valid
                                       // array.

_DecimalT<4,2> arr_1[] = {__D("12.35"), __D("25.00"),
                                              __D("-19.58")};
_DecimalT<4,2> arr_2[N];

void write_num(_DecimalT<4,2> a[N]); //Declare function to
                                      // write to a file.

void read_num(_DecimalT<4,2> b[N]); //Declare function to
                                       //read from a file.

int main ( void )
{
write_num(arr_1);               // Call function to write
                                    // values of the
                                    // array to outf with fprintf
                                    // library function.

read_num(arr_2);                    // Call function to read
                                         // values of the
                                         // array from file using
                                         // fscanf() function.
}
// write_num is passed an array.  These values are
// written to a text file with the fstream class.
// If the function is successful a 0 is returned, otherwise a
// negative value is returned (indicating an error).

void write_num(_DecimalT<4,2> a[N])
{
  int i;
   ofstream outf("data",ios::trunc || ios::out,
                        filebuf::openprot);
   if (!outf)
      {
        cerr << "Could not open file 'data' " <<endl;
        exit (EXIT_FAILURE);
      }
   for (i=0; i < N; i++) {
     {
        outf << a[i] << endl;
     }
     outf.close()
}
// read_num is passed an array.  The values are
// read from a text file with the fstream class.
// If the function is successful a 0 is returned, otherwise a
// negative value is returned (indicating an error).

void read_num(_DecimalT<4,2> b[N])
{
   int i;
   ifstream file("data");

   if (!file)
      {
        cerr << "Could not open file 'data' " <<endl;
        exit (EXIT_FAILURE);
      }
for (i=0; i<N; i++)
      {
        file >> b[i];
        cout << "b["<< i <<"]=" <<b[i] <<endl;
      }
   if (file.eof())
      {
        cerr << "Unexpected EOF!" <<endl;
        exit (EXIT_FAILURE);
       }
       file.close();
}
输出为:
b[0]=12.35
b[1]=25.00
b[2]=-19.58