Connecting Tech Pros Worldwide Help | Site Map

binary fileoutput

  #1  
Old July 19th, 2005, 06:27 PM
J. Campbell
Guest
 
Posts: n/a
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;
}
  #2  
Old July 19th, 2005, 06:27 PM
Gianni Mariani
Guest
 
Posts: n/a

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 ?


  #3  
Old July 19th, 2005, 06:27 PM
Kevin Goodsell
Guest
 
Posts: n/a

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.

Closed Thread