Next: , Previous: Array globals, Up: Arrays


2.8 Inputting and Outputting Arrays

2.8.1 Output formatting

The current version of Blitz++ includes rudimentary output formatting for arrays. Here's an example:

     #include <blitz/array.h>
     
     using namespace blitz;
     
     int main()
     {
         Array<int,2> A(4,5,FortranArray<2>());
         firstIndex i;
         secondIndex j;
         A = 10*i + j;
     
         cout << "A = " << A << endl;
     
         Array<float,1> B(20);
         B = exp(-i/100.);
     
         cout << "B = " << endl << B << endl;
     
         return 0;
     }
     

And the output:

     A = (1,4) x (1,5)
     [ 11 12 13 14 15
       21 22 23 24 25
       31 32 33 34 35
       41 42 43 44 45 ]
     
     B =
     (0,19)
     [ 1 0.99005 0.980199 0.970446 0.960789 0.951229 0.941765 0.932394 0.923116
     0.913931 0.904837 0.895834 0.88692 0.878095 0.869358 0.860708 0.852144 0.843665
     0.83527 0.826959 ]
     

2.8.2 Inputting arrays

Arrays may be restored from an istream using the >> operator.

Caution: you must know the dimensionality of the array being restored from the stream. The >> operator expects an array in the same input format as generated by the << operator, namely:

The operator prototype is:

     template<class T, int N>
     istream& operator>>(istream&, Array<T,N>&);

Here is an example of saving and restoring arrays from files. You can find this example in the Blitz++ distribution as examples/io.cpp.

     #include <blitz/array.h>
     #ifdef BZ_HAVE_STD
     	#include <fstream>
     #else
     	#include <fstream.h>
     #endif
     
     BZ_USING_NAMESPACE(blitz)
     
     const char* filename = "io.data";
     
     void write_arrays()
     {
         ofstream ofs(filename);
         if (ofs.bad())
         {
             cerr << "Unable to write to file: " << filename << endl;
             exit(1);
         }
     
         Array<float,3> A(3,4,5);
         A = 111 + tensor::i + 10 * tensor::j + 100 * tensor::k;
         ofs << A << endl;
     
         Array<float,2> B(3,4);
         B = 11 + tensor::i + 10 * tensor::j;
         ofs << B << endl;
     
         Array<float,1> C(4);
         C = 1 + tensor::i;
         ofs << C << endl;
     }
     
     int main()
     {
         write_arrays();
     
         ifstream ifs(filename);
         if (ifs.bad())
         {
             cerr << "Unable to open file: " << filename << endl;
             exit(1);
         }
     
         Array<float,3> A;
         Array<float,2> B;
         Array<float,1> C;
     
         ifs >> A >> B >> C;
     
         cout << "Arrays restored from file: " << A << B << C << endl;
     
         return 0;
     }
     

Caution: The storage order and starting indices are not restored from the input stream. If you are restoring (for example) a Fortran-style array, you must create a Fortran-style array, and then restore it. For example, this code restores a Fortran-style array from the standard input stream:

     Array<float,2> B(fortranArray);
     cin >> B;