On storing tick data in binary files with C++

How often do you write to the binary file? Every tick? Every 2000 ticks?

Would anyone be willing to post some code?

How does this look?

Code:
ofstream myfile;
myfile.open("data.bin", std::ios::out | std::ios::binary);
myfile.write((const char*) &myData, sizeof(myData));
myfile.close();

Then I'd just have to record my position and every couple thousand ticks add to the binary file?
 
@bellman

yep, that looks good.

buffer it based on resources... 2k ticks sounds fine though. you can also wrap it up in a low priority thread.
 
@propseeker, thank you. using a low priority thread is a good idea. i'll have to implement that once i get everything working.
 
Quote from uexkuell:

It might be worth thinking about keeping the file(s) open.
Reduces overhead from repeatedly doing the open/close.

This. Plus, most OSs have a virtual filesystem layer that allows a file to be transparently cached in memory so that when you write it repeatedly those writes get combined automatically before they go to disk. I don't know the details of what the OS and programming environment is here, but I would certainly not expect to have to write that code myself.
 
ahh, thank you very much. I'm asking because the first go around I tried writing to a csv file at the end of the day, and of course my program crashed.
 
This may sound like an off-topic question:

Do you have more experience with other languanges (like Basic, Java..)?

Might make work much easier.
 
Back
Top