Thanks for the info on storing things into the i/ostream directly, that could come in useful elsewhere

, but, the reason I wanted to determine the open mode is to stop a method being inadvertently called with a stream in the wrong mode.. so, that won't work.. but I seem to get the same results in light testing when writing to a stream opened in 'the wrong mode' anyway.. hence my second question.
The endian-ness thing, I'm still not sure I get it... a more specific example of what I'm doing, writing 32-bit floats to and from binary files, I don't have access to a machine with a different endian-ness.. But, lets say I work on a PC, and I want my files to open in a Mac ( which is apparently reverse endianess to PC ).. [ I am happy to assume that floats are 32-bit on my targets.. and that they're represented the same... although, is that even a safe assumption on Windows, Mac, Linux? See I really don't want to have to resort to a dodgy handrolled file representation for non-integers, and using a text representation for floating point arrays never really appealled to me. ]
Anyway, assuming that the representation is the same.. if I do this:
int main( void )
{
std::ofstream out( "test.bin", std::ios::binary | std::ios::out | std::ios::trunc );
const float f1 = 123.456f;
out.write( reinterpret_cast< const char * >( &f1 ), sizeof( float ) );
out.close( );
std::ifstream in( "test.bin", std::ios::binary | std::ios::in );
float f2 = 0.0f;
in.read( reinterpret_cast< char * >( &f2 ), sizeof( float ) );
in.close( );
assert( f1 == f2 );
}
Will it work ( i.e. will the read value be equal the written value ) if the second half of that process is done on a reverse-endian machine? Or is it better ( or no change atall ) to do this?
int main( void )
{
std::ofstream out( "test.bin", std::ios::binary | std::ios::out | std::ios::trunc );
const float f1 = 123.456f;
const char * c1 = reinterpret_cast< const char * >( &f1 );
for( size_t i = 0; i < sizeof( float ); ++i ) {
out.put( c1[ i ] );
}
out.close( );
std::ifstream in( "test.bin", std::ios::binary | std::ios::in );
float f2 = 0.0f;
char * c2 = reinterpret_cast< char * >( &f2 );
for( size_t i = 0; i < sizeof( float ); ++i ) {
c2[ i ] = in.get( );
}
in.close( );
assert( f1 == f2 );
}
Or, is it necessary to reverse the order of the bits in each byte read if it is determined that the machine reading the file is 'backwards' ?