Creating and Using Streams With Only One Streaming Element
The first example shows how to create and use a single streaming element. It creates an instance of class
RWByteToStreambufOutputStreamImp, which uses an instance of the iostreams class
filebuf to write to a file. The complete example is located in directory
...\examples\stream in the file
binaryTerminalWrite.cpp. Only part of the code is presented below.
filebuf fbuf; // 1
fbuf.open("binaryTerminalWrite.dat",ios::out | ios::binary);
RWByteOutputStream binOutputStream =
RWByteToStreambufOutputStreamImp::make(fbuf); // 2
RWByte array_[10]; // 3
unsigned char i;
for(i=0; i<10; i++);
array_[i]= i+64;
// fill up the array with values
try {
for(i=0; i<10; i++)
binOutputStream << array_[i]; // 4
for(i=0; i<10; i++)
binOutputStream.write(array_[i]); // 5
binOutputStream.write(array_,10); // 6
}
catch(const RWIncompleteStreamOperation& e) { // 7
cout << e.why() << endl;
cout << e.elementsProcessed() << endl;
}
catch(const RWExternalStreamException& e) {
cout << e.why() << endl;
}
binOutputStream << array_[0] << array_[1] << array_[2]
<< rwFlush;
The next example implements the previous example’s corresponding input operation. It creates an instance of class
RWByteFromStreambufInputStreamImp, which uses an instance of the iostreams class
filebuf to read from the file written in the previous example. The complete example is located in directory
...\examples\stream in the file
binaryTerminalRead.cpp. Only part of the code is presented below.
filebuf fbuf; // 1
fbuf.open("binaryTerminalWrite.dat", ios::in | ios::binary);
RWByteInputStream binInputStream =
RWByteFromStreambufInputStreamImp::make(fbuf); // 2
try {
RWSize i;
RWByte theByte; // 3
for(i=0; i<10; i++) {
binInputStream >> theByte; // 4
// do something with the value read
}
for(i=0; i<10; i++) {
theByte= binInputStream.read(); // 5
// do something with the value read
}
RWByte array_[10];
RWSize read_= binInputStream.read(array_,10); // 6
// do something with the array of bytes
RWByte until_= 69;
read_= binInputStream.readUntil(array_,10,until_); // 7
if(binInputStream.isGood()) // 8
cout << "The operation succeeded\n";
cout << "The number of byte(s) stored is: " << read_ << endl;
// do something with the bytes stored
if (binInputStream.isGood()) // 8
cout << "The stream is in a valid state" << endl;
else
cout << "The stream is in error state" << endl;
if(binInputStream.isEof()) // 9
cout << "There is no more data available for reading"
<< endl;
else {
cout << "More data is available for reading" << endl;
while(!binInputStream.isEof()) // 9
theByte= binInputStream.read();
// do something with the byte value
} //end else
} // end try
catch(const RWExternalStreamException& e) { // 10
cout << e.why() << endl;
}
RWByte value1, value2, value3;
binInputStream >> value1 >> value2 >> value3;
Two more examples demonstrating how to create and use single streaming element can be found in directory
examples\stream in the files
narrowCharacterTerminalWrite.cpp and
narrowCharacterTerminalRead.cpp. The first example creates an instance of class
RWCharToStreambufOutputStreamImp, which uses an instance of the -iostreams class
filebuf to write to a file. The second example creates an instance of class
RWCharFromStreambufInputStreamImp, which uses an instance of the -iostreams class
filebuf to read from the file written in the previous example.