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