Thank you for your replies. I have allready found out what i needed to know. If someone's interested, here's what I meant:
Code:
unsigned long foo = 1234567;
myfilestream myfile;
myfile << foo << " "<< std::hex << foo; // assume myfile to be an open ofstream
myfile would now contain
Code:
"1234567 0012d687" (I don't know if it uses big endian...)
But that is bad, because sometimes you want to write the data like it appears in system memory.
You could write instead:
Code:
myfile.write(reinterpret_cast<myfilestream::char_type*>(&foo), sizeof(foo));
/*which produces on big endian machines*/
"‡Ö "
But that all wasn't a solution for my problem:
Code:
class foo
{
friend istream& operator >> (istream&, foo&);
/*...*/
// data members:
unsigned long _start;
unsigned long _end;
};
// assume using namespace std;
istream& operator >> (istream& in, foo& f)
{
in >> f._start;
in >> f._end;
if (f._start > f._end) swap(f._start, f._end);
return in;
}
class bar
{
istream& operator >> (istream& in, bar&)
/*...*/
// own data
unsigned long _id;
foo _location;
}
istream& operator >> (istream& in, bar& b)
{
in >> b._id;
in >> f._location; // CALLS operator>>(/*...*/) for _location
return in;
}
foo's operator<< checks for disordered start/end values and swaps them if necessary.
If i had just used
Code:
in.read(reinterpret_cast<istream::char_type*>(&bar), sizeof(bar));
that check was never performed, because operator >> was never called for _location (which is a foo and has it's own overloaded verson of >>).
But i can't encode my files in ascii, and streams will allways output as ascii, except:
The stream's local's facets num_put/num_get are replaced by your own ones, that just perform a raw memory write.
Those classes perform formattion of numbers. Search your stl / std lib documentation for more information ( header <locale> ).
That's pretty handy for debugging as well because you can switch between ascii and raw representation.