473,698 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Faster way to write binary using fstream??

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::tr unc|ios::binary );

file_out.seekg( 0);
for(i=0; i<iLength; i++) // <-- very slow!!
{
unsigned char b;
b = pDataOut[i];
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::tr unc|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?
Jul 22 '05 #1
4 3485
> 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..
Jul 22 '05 #2
Jon Hyland wrote:
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::tr unc|ios::binary );

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

So why don't you write bigger chunks instead of char-at-a-time ?
Should speed things up.
Jul 22 '05 #3

"Jon Hyland" <jo*******@hotm ail.com> wrote in message
news:f2******** *************** ***@posting.goo gle.com...
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::tr unc|ios::binary );

file_out.seekg( 0);
for(i=0; i<iLength; i++) // <-- very slow!!
{
unsigned char b;
b = pDataOut[i];
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::tr unc|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(pszOut File, ios::in|ios::tr unc|ios::binary );
file_out.write( pDataOut, iLength);

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

john
Jul 22 '05 #4
> > 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(pszOut File, ios::in|ios::tr unc|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 :)
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1308
by: Esa Komulainen via .NET 247 | last post by:
Sorry if my english sucks, it's not my native language. I'm stuck with following problem: I have two vb.net (2003) applications, first one makes a list ofwords and their occurrence: Dim formatter As NewSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter Dim fstream As New System.IO.FileStream _ (txtPath.Text, IO.FileMode.Create) formatter.Serialize(fstream, wordlist) fstream.Close()
7
2556
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about it's buisness. My problem is all the resources I have found aren't very clear on these things. All of them open the file then check to see if the stream is open. Well, the problem with using file.open("filename.dat", ios::in | ios::binary) is...
6
3286
by: Jon Slaughter | last post by:
I'm trying to replace some bytes in a file using fstream but all I seem to be able to do is append or 0 the file then write... Basicaly I just need to replace the first 512 bytes of the file with some buffer(but eventually I will need to replace random blocks in the file at different locations). Is there anyway to do this efficiently using fstream? (I know I can duplicate the file, but it seems a waste to rewrite the same data for no...
3
2123
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop the program (using "Stop debugging"). That is writing 552 bytes. The resulting file's properties...
6
17177
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
1
5485
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and write using fstream is not not working.If i close the file and open it again it works. #include <iostream> #include <sys/stat.h> #include <fstream> int main(){ fstream fs; char write= {"A0000.label"};
31
14499
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out that the read and get functions do not accept float type. What should i do now? Can anybody help me please? Here is the code.. #include <fstream> #include <iostream> using std::cout;
6
3755
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv) { fstream fsBin; fsBin.open(argv, fstream::in | fstream::binary);
1
3925
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4361
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.