binary fileoutput 
July 19th, 2005, 05:27 PM
| | | binary fileoutput
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? Also, how can I use the
function return value directly in out.write, rather than having to go
through the temp variable?
Thanks,
Joe
#include <fstream>
using namespace std;
#include "worker.h"
int main(){
int data_in = 10;
unsigned long temp;
ofstream out("file.dat", ios::out|ios::binary);
{
Worker Bob(data_in);
for (int i = 0; i < 100; ++i){
temp = Bob.get_data(); //returns unsigned long;
out.write(reinterpret_cast<char*>(&temp), sizeof(temp));
}
} //bob's day is done...have a nice weekend, bob
return 0;
} | 
July 19th, 2005, 05:27 PM
| | | 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? Also, how can I use the
> function return value directly in out.write, rather than having to go
> through the temp variable?
>
> Thanks,
>
> Joe
>
> #include <fstream>
> using namespace std;
> #include "worker.h"
>
> int main(){
> int data_in = 10;
>
> unsigned long temp;
> ofstream out("file.dat", ios::out|ios::binary);
> {
> Worker Bob(data_in);
> for (int i = 0; i < 100; ++i){
> temp = Bob.get_data(); //returns unsigned long;
> out.write(reinterpret_cast<char*>(&temp), sizeof(temp));
> }
> } //bob's day is done...have a nice weekend, bob
> return 0;
> }[/color]
class Writer
{
ostream & output;
public:
Writer( ostream & out )
: output( out )
{
}
template <typename T>
Writer & operator << ( const T& value )
{
out.write(reinterpret_cast<char*>(&value), sizeof(T));
return * this;
}
};
Would somthing like this help ?
Writer bfile( out );
bfile << Bob.get_data();
..... how are you going to deal with endian issues ? | 
July 19th, 2005, 05:27 PM
| | | 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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,840 network members.
|