-
Notifications
You must be signed in to change notification settings - Fork 16
DA input and output
Alexander Wittig edited this page Feb 15, 2019
·
2 revisions
I/O operations in the DACE C++ interface blend in nicely with the C++ stream I/O concept. You can use the stream insertion operator <<
to write a human readable ASCII representation of any DA object to any output stream (the console, a file, a network socket, ...). Similarly, the stream extraction operator >>
allows you to read a DA object back from an input stream.
In addition to the ASCII representation, there is also a binary representation which is lossless and smaller. This can be written to a stream by converting a DA
object to a storedDA
object before writing. When reading, the stream extraction operator >>
transparently reads both types of objects.
#include <dace/dace.h>
#include <fstream>
using namespace std;
using namespace DACE;
int main( void )
{
DA::init( 20, 1 );
DA x = DA::random(1.0);
DA y = sin(x);
// output x and y to file
ofstream os("test.dat", ofstream::binary); // Windows requires binary flag if this stream is used to write binary DA representations ("stored DAs")
os << x // output DA x as a human readable ASCII representation
<< storedDA(y); // and DA y as a compact binary representation
os.close();
// read values back
DA xx, yy;
ifstream is("test.dat", ifstream::binary); // same here: Windows needs to be told explicitly about binary files
is >> xx >> yy; // same stream extraction operator handles both binary and ASCII for reading
is.close();
// show differences
cout << "Difference between x and read value:\n" << x-xx << endl;
cout << "Difference between y and read value:\n" << y-yy << endl;
}