However, this did not happen! the first attribute "Code" size = 1 byte, when placed in the file occupies 4 bytes instead of 1, pushing "time" for the fifth position of the file
It is because you are playing with a class and not a POD. Google around "c++ serialization" for more.
In short, you should explicitly read/write every field from/to a binary file, and make sure to byte-format them yourself. You can do this through simple member functions.
Here are some nice functions for reading/writing integer values in little-endian format:
1 2 3 4 5 6 7
template <typename Word>
ostream& write_word( ostream& outs, Word value )
{
for (unsigned size = sizeof( Word ); size; --size, value >>= 8)
outs.put( static_cast <char> (value & 0xFF) );
return outs;
}
1 2 3 4 5 6 7
template <typename Word>
istream& read_word( istream& ins, Word& value )
{
for (unsigned size = 0, value = 0; size < sizeof( Word ); ++size)
value |= ins.get() << (8 * size);
return ins;
}
Now, read each field separately as already recommended to you:
Things to remember:
- The serialno and subversao fields presume that the string is properly null-terminated in the file, or it simply does not require it. (This is exactly as it is with your original code).
- The ruf array could have been read with istream::read() also, but I put in the example in case you find yourself loading an array of WORD or DWORD.
- Be sure to check your input stream for errors afterwards... the read_word() doesn't report them, but if, for example, you hit EOF before an entire word (BYTE, WORD or DWORD) is read, the result will not have a useful value.