Chapter 49
IO Streams

Andreas Fabri, Geert-Jan Giezeman, and Lutz Kettner

All classes in the CGAL kernel provide input and output operators for IO streams. The basic task of such an operator is to produce a representation of an object that can be written as a sequence of characters on devices as a console, a file, or a pipe. In CGAL we distinguish between a raw ascii, a raw binary and a pretty printing format.

enum Mode { ASCII = 0, BINARY, PRETTY};

In ASCII mode, objects are written as a set of numbers, e.g. the coordinates of a point or the coefficients of a line, in a machine independent format. In BINARY mode, data are written in a binary format, e.g. a double is represented as a sequence of four byte. The format depends on the machine. The mode PRETTY serves mainly for debugging as the type of the geometric object is written, as well as the data defining the object. For example for a point at the origin with Cartesian double coordinates, the output would be PointC2(0.0, 0.0). At the moment CGAL does not provide input operations for pretty printed data. By default a stream is in ASCII mode.

CGAL provides the following functions to modify the mode of an IO stream.

IO::Mode set_mode ( std::ios& s, IO::Mode m)

IO::Mode set_ascii_mode ( std::ios& s)
IO::Mode set_binary_mode ( std::ios& s)
IO::Mode set_pretty_mode ( std::ios& s)

The following functions allow to test whether a stream is in a certain mode.

IO::Mode get_mode ( std::ios& s)

bool is_ascii ( std::ios& s)
bool is_binary ( std::ios& s)
bool is_pretty ( std::ios& s)

49.1   Output Operator

CGAL defines output operators for classes that are derived from the class ostream. This allows to write to ostreams as cout or cerr, as well as to strstreams and fstreams. The output operator is defined for all classes in the CGAL kernel and for the class Color as well. Let os be an output stream.

ostream& ostream& os << Class c
Inserts object c in the stream os. Returns os.

Example


#include <CGAL/basic.h>
#include <iostream>
#include <fstream>

#include <CGAL/Cartesian.h>
#include <CGAL/Segment_2.h>

typedef CGAL::Point_2< CGAL::Cartesian<double> >     Point;
typedef CGAL::Segment_2< CGAL::Cartesian<double> >   Segment;

int main()
{
    Point p(0,1), q(2,2);
    Segment s(p,q);

    CGAL::set_pretty_mode(std::cout);
    std::cout << p << std::endl << q  << std::endl;

    std::ofstream f("data.txt");
    CGAL::set_binary_mode(f);
    f << s << p ;

    return 1;
}

49.2   Input Operator

CGAL defines input operators for classes that are derived from the class istream. This allows to read from istreams as cin, as well as from strstreams and fstreams. The input operator is defined for all classes in the CGAL kernel. Let is be an input stream.

istream& istream& is >> Class c
Extracts object c from the stream is. Returns is.

Example


#include <CGAL/basic.h>
#include <iostream>
#include <fstream>

#include <CGAL/Cartesian.h>
#include <CGAL/Segment_2.h>

typedef CGAL::Point_2< CGAL::Cartesian<double> >     Point;
typedef CGAL::Segment_2< CGAL::Cartesian<double> >   Segment;

int
main()
{
    Point p, q;
    Segment s;

    CGAL::set_ascii_mode(std::cin);
    std::cin >> p >> q;

    std::ifstream f("data.txt");
    CGAL::set_binary_mode(f);
    f >> s >> p;

    return 1;
}

49.3   Stream Support

Three classes are provided by CGAL as adaptors to input and output stream iterators. The class Istream_iterator is an input iterator adaptor and is particularly useful for classes that are similar but not compatible to std::istream. Similarly, the class Ostream_iterator is an output iterator adaptor. The class Verbose_ostream can be used as an output stream. The stream output operator << is defined for any type. The class stores in an internal state a stream and whether the output is active or not. If the state is active, the stream output operator << uses the internal stream to output its argument. If the state is inactive, nothing happens.