binary fileoutput

J

J. Campbell

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::eek:ut|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;
}
 
G

Gianni Mariani

J. Campbell said:
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::eek:ut|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;
}


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 ?
 
K

Kevin Goodsell

J. Campbell said:
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?

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.
Also, how can I use the
function return value directly in out.write, rather than having to go
through the temp variable?

You can't. You need the address, so you have to put the data in
something that has an address.

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top