| re: binary fileoutput
J. Campbell wrote:
[color=blue]
> I have an object with a member function that I'm using to generate a
> stream of binary data that, rather than using, I want to output
> directly to a file. It has a member function that returns an unsigned
> long. Is there a standard C++ way to accomplish this without using
> the ugly out.write(reinterpret_cast... line?[/color]
Not one that's any less ugly. But you could wrap it in a function instead:
ostream& WriteLong(ostream& os, long a)
{
os.write(reinterpret_cast<char*>(&a), sizeof(a));
return os;
}
Now you just call it with
WriteLong(out, temp);
But note that this method of writing data does not create portable
files. The size and byte ordering for long is implementation-dependent.
You might be better off rewriting WriteLong to take these things into
account, and make the code and data files portable.
[color=blue]
> Also, how can I use the
> function return value directly in out.write, rather than having to go
> through the temp variable?[/color]
You can't. You need the address, so you have to put the data in
something that has an address.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. |