Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 803
News FAQ
Search Home


next up previous contents index
Next: The include file Up: An advanced example Previous: An advanced example

The main program

We start with the main program which will simply call a couple of routines to check the coding (The source code for this program is found in $AIPSROOT/code/trial/implement/test/tExample3.cc).

 1  #include <aips/aips.h>
 2  #include <aips/AipsIO.h>
 3  #include <aips/ArrayIO.h>
 4  #include <aips/Input.h>
 5  #include <aips/Math.h>
 6  #include <aips/Matrix.h>
 7  #include <aips/AsciiFileIO.h>
 9  
10  main(int argc, char **argv)
11  {
12   Input inputs(1);
13   inputs.Version("$w{Revision}$");
14   inputs.Usage("Testing Ascii IO routines");
15   inputs.Create("infile1","Input1","Float Input file name?","Infile");
16   inputs.Create("infile2","Input2","Int Input file name?","Infile");
17   inputs.Create("infile3","Input3","Double Input file name?","Infile");
18   inputs.Create("outfile","Output","Output file name?","Outfile");
19   inputs.ReadArguments(argc, argv);
20  
21   const char *filein1 = inputs.GetString("infile1");
22   const char *filein2 = inputs.GetString("infile2");
23   const char *filein3 = inputs.GetString("infile3");
24   const char *fileout = inputs.GetString("outfile");
25  
26   Int rows, cols;
27   try {
28      Matrix <Float> mat1;
29      Matrix <Int> mat2;
30      Matrix <Double> mat3;
31  
32      ReadAsciiFile(filein1, mat1);
33      mat1.shape(rows, cols);
34      cout << "The first Matrix is " << rows << " by " << cols << endl;
35  
36      ReadAsciiFile(filein2, mat2);
37      mat2.shape(rows, cols);
38      cout << "The second Matrix is " << rows << " by " << cols << endl;
39  
40      ReadAsciiFile(filein3, mat3);
41      mat3.shape(rows, cols);
42      cout << "The third Matrix is " << rows << " by " << cols << endl;
43  
44      WriteAsciiFile(fileout, mat1);
45  
46      cout.precision(12);
47      cout << mat1 << mat2 << mat3;
48     }
49  
50   return 0;
51
52    catch (AipsError x) {
53    cerr << "aipserror: error " << x.getMesg() << endl;
54          return 1;
55         }
56
58  }

The following discussion refers to the line numbers which have been prepended to the code above:

7-
This line includes the new routines in the code for AsciiFileIO.

15-17-
The program will read 3 different arrays of differing characteristics.

28-30-
The Matrices are declared to be of three differing types but the sizes of the arrays is not stated. It will be set dynamically by the subroutines.

32-
The ReadAsciiFile function is called to read a matrix of floating point values. The type is specified in the declaration of mat1 which is used as an argument. The size of the array is still unknown. Note that the data in the array may be in integer form but it will be stored as an array of float values.

33-
The member function shape is called to determine the dimensions of the matrix that was just read.

36-
The same new function, ReadAsciiFile, is called to read an array of integers. The data may be specified with decimal points and fractional parts but they will be placed in the matrix as integers.

37-
The member function shape is used to get the dimensions of the matrix mat2. Note that these dimensions are quite independent of the dimesions found for the matrix mat1.

40-41-
The same comments hold for a matrix of doubles.

44-
The routine to write the array, WriteAsciiFile, need not pass the dimensions of the array since they can be determined in the same way as they were in lines 33, 37 and 41.

46-
The property of cout, precision, is used to set the number of decimal places that will appear in the output (where they are not all zero). Other properties of the format state may also be set. For example, one could set the number of decimal places displayed to a constant (regardless of whether or not they were all zeroes) by using the statement cout.setf(ios::showpoint).


next up previous contents index
Next: The include file Up: An advanced example Previous: An advanced example   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2004-08-30