Seems a combination of what Mr V and I suggested should do the trick?
Indeed. This is what I have gone with, using a const cast for simplicity but
which could break if compiled with an implementation of std::string that does
things differently: (which, if it ever happens, will at least be picked up by
the unit tests)
struct fixedLength {
std::string& data;
int len;
fixedLength(std::string& data, int len) :
data(data),
len(len)
{
}
};
std::istream& operator >> (std::istream& s, const fixedLength& n)
{
n.data.resize(n.len);
s.read(const_cast<char *>(n.data.c_str()), n.len);
return s;
}
Then it can be used as:
std::string foo;
stream >> fixedLength(foo, 123);
I'll probably have to create another class for the times when I want to read
in the same amount of data but have the string stop at the first null.
Thanks again both for your suggestions.
Cheers,
Adam.