SourcePro : Essential Math Module User’s Guide : Examples : Example with saveOn()and restoreFrom()
Example with saveOn()and restoreFrom()
Example 5 – Using saveOn() and restoreFrom()
FILENAME: example5.cpp
Program:
/*
* Example program showing the use of the member
* functions saveOn() and restoreFrom() for data
* storage and retrieval.
*/
 
#include <rw/math/mathvec.h>
#include <rw/pstream.h>
#include <fstream.h>
 
int main()
{
/*
* Construct an integer vector a, with 24 elements.
* The first element has value 0; each succeeding element
* is incremented by 1.
*/
 
RWMathVec<int> a(24, 0, 1);
 
// Store the vector to file "vec.dat" using class
// RWpostream which saves in a portable ASCII format.
{
ofstream fstr("vec.dat", ios::out);
RWpostream postr(fstr);
a.saveOn(postr);
}
 
// A vector that has been saved using function saveOn()
// may be restored using restoreFrom().
 
ifstream fstr("vec.dat", ios::in);
// Construct a RWpistream from fstr:
RWpistream pistr(fstr);
RWMathVec<int> b;
b.restoreFrom(pistr); // Restore from file "vec.dat"
 
std::cout << a << std::endl; // Print the original 'a'
std::cout << b << std::endl; // Print the restored 'b'
}
Program Output:
[
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23
]
[
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23
]
 
You may also want to look at file vec.dat to see the storage format.