|Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.|
Input and Output StreamsDescription: General usage notes for input-output streams.
Keywords: ecl streams
Tutorial Level: INTERMEDIATE
Ecl streams work on top of ecl io devices.
Each stream-device pair can be instantiated via the generic TextStream object and the underlying device opened via the device() member method. For example,
The console device is a special case - it is automatically opened.
The preferred method of instantiation is via the constructor in the convenience classes. The convenience classes are specialised interfaces that inherit from the appropriate Textstream type (dependant on the template parameter). Examples, SerialStream, OFileStream. Rather than calling stream.device().open(...) directly, you can use their constructors to instantiate the stream. They are also more convenient than typing out template parameters continuously.
1 OFileStream stream("dude.txt",New);
Output streams function similarly to the familiar cout stream. The primary advantage being that they can easily be attached to any standard ecl io device. They can also be used with the ecl formatter classes.
There are three ways to utilise an input stream:
- reading element by element (separated by whitespace or newlines).
- reading raw characters (you must call enableRawCharReads()).
- reading line by line (not yet enabled).
The first method is similar to the familiar cin, it will take a string and convert it to the requested type.
The second method is sometimes useful when communicated with raw character devices (e.g. a serial line). In this situation, you must simply make use of the char input operator.
The third method has not yet been implemented.
- Output streams can generate errors that are not so easily checked compared with handling devices directly. To check for failure, ecl streams use a mechanism similar to that of the standard cout stream.