Faster way to write binary using fstream??

J

Jon Hyland

Hi all,

I'm looking for the fastest way to write (and/or read) binary to a
file in VC++. I've been using the fstream object in this way:

//unsigned char *pDataOut and long iLength initilized somewhere..

fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);

file_out.seekg(0);
for(i=0; i<iLength; i++) // <-- very slow!!
{
unsigned char b;
b = pDataOut;
file_out.write(&b, sizeof(b));
}

file_out.close();


Code above works every time but it's heartbreakingly slow. I know
there's got to be a better way to do this. I've tried this (below)
method, which is 10x faster, but I get weird behavior. When I write
the binary back out to a file it is truncated at around 1KB, but only
sometimes. Other times it works fine and I can't explain it:

//unsigned char *pDataOut initilized somewhere..

fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);

file_out.seekg(0);
file_out << pDataOut; // <-- much faster, but unpredictable.. why??

file_out.close();


Does anyone know the *best* way to write binary to a file?
 
A

assaarpa

file_out << pDataOut; // <-- much faster, but unpredictable.. why??

What you think?

const char text[] = "blahblah";
file_out << text;

What you predict will happen? Now, what you predict will happen when you
have char* and << it to the file_out object? Doh! Try ::write method, good
luck..
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Jon said:
Hi all,

I'm looking for the fastest way to write (and/or read) binary to a
file in VC++. I've been using the fstream object in this way:

//unsigned char *pDataOut and long iLength initilized somewhere..

fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);

file_out.seekg(0);
for(i=0; i<iLength; i++) // <-- very slow!!
{
unsigned char b;
b = pDataOut;
file_out.write(&b, sizeof(b));
}

So why don't you write bigger chunks instead of char-at-a-time ?
Should speed things up.
 
J

John Harrison

Jon Hyland said:
Hi all,

I'm looking for the fastest way to write (and/or read) binary to a
file in VC++. I've been using the fstream object in this way:

//unsigned char *pDataOut and long iLength initilized somewhere..

fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);

file_out.seekg(0);
for(i=0; i<iLength; i++) // <-- very slow!!
{
unsigned char b;
b = pDataOut;
file_out.write(&b, sizeof(b));
}

file_out.close();


Code above works every time but it's heartbreakingly slow. I know
there's got to be a better way to do this. I've tried this (below)
method, which is 10x faster, but I get weird behavior. When I write
the binary back out to a file it is truncated at around 1KB, but only
sometimes. Other times it works fine and I can't explain it:

//unsigned char *pDataOut initilized somewhere..

fstream file_out((const char *)pszOutFile,
ios::in|ios::trunc|ios::binary);

file_out.seekg(0);
file_out << pDataOut; // <-- much faster, but unpredictable.. why??


Because its meant for null terminated string, not binary data.

Do it like this

fstream file_out(pszOutFile, ios::in|ios::trunc|ios::binary);
file_out.write(pDataOut, iLength);

seekg(0) is completely unnecessary, and maybe the cast of pszOutFile is too.

john
 
J

Jon Hyland

file_out << pDataOut; // <-- much faster, but unpredictable.. why??
Because its meant for null terminated string, not binary data.

Do it like this

fstream file_out(pszOutFile, ios::in|ios::trunc|ios::binary);
file_out.write(pDataOut, iLength);

seekg(0) is completely unnecessary, and maybe the cast of pszOutFile is too.

john

Thanks man, I managed to miss the obvious. I'll give it a shot,
though I'm sure it'll work fine.

BTW - I figured seekg(0) wasn't necessary but just feels safe :)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top