I have a data structure defined as
struct myDataStruct
{
int32_t header;
int16_t data[8];
}
and I want to take a character stream and turn it into a myData stream. What stream class should I extend? I would like to create a custom stream class so that I can do things like
myDataStruct myData;
myDataStruct myDataArray[10];
myDataStream(ifstream("mydatafile.dat"));
myDataStream.get(myData);
myDataStream.read(myDataArray, 10);
Instead of
myDataStream.get(myData), what you do is overloadoperator>>for your data type:If you want to read into an array, just write a loop:
Using a function template, you should also able to overload the operator for arrays on the right-hand side (but this I have never tried):
But I can’t decide whether this is gorgeous or despicable.