On storing tick data in binary files with C++

Discussion in 'Data Sets and Feeds' started by bellman, Sep 13, 2010.

  1. bellman

    bellman

    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?
     
  2. byteme

    byteme

  3. @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.
     
  4. bellman

    bellman

    @propseeker, thank you. using a low priority thread is a good idea. i'll have to implement that once i get everything working.
     
  5. It might be worth thinking about keeping the file(s) open.
    Reduces overhead from repeatedly doing the open/close.
     
  6. 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.
     
  7. bellman

    bellman

    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.
     
  8. bellman

    bellman

    Okay, this might sound like a dumb question, but I've been banging my head. How do I read the data in the binary file back into a type double?

    I've tried with the ifstream class read function and posted to DaniWeb http://www.daniweb.com/forums/thread311612.html , but I don't have a solution. Can anyone help me out?
     
  9. This may sound like an off-topic question:

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

    Might make work much easier.
     
  10. bellman

    bellman

    No, I'm just a newb all the way around. I am equally bad at several languages.
     
    #10     Sep 15, 2010