Like Tree1Likes
  • 1 Post By Salem

Saving Temporary File

This is a discussion on Saving Temporary File within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I need to save data from my program at certain intervals (in case my program gets terminated/evicted). In ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Talking Saving Temporary File

    Hi guys,

    I need to save data from my program at certain intervals (in case my program gets terminated/evicted).

    In order for the data to be saved... must the file be closed?

    Is there an efficient way to do this or just what I am thinking:

    At given intervals:
    -Check if temp file is open... open the file
    -write data to file
    -close the file
    - ... wait for next interval

    Any ideas/helps?

    Many thanks

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    If the data has a fairly structured format and the file size does not arbitrarily change ((what happens is the newer file is smaller in size than the older?)) , you can seek to ios::beg and dump the entire contents at the intervals.
    To be more sophisticated, you have to keep track of all changes during the intervals and seek to the necessary stream positions to update.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks manasij I'll bear that in mind.

    The other thing is:

    I will be saving 3 data files. Each one contains crucial information for re-starting the program from the last save point (incase it gets cut off).

    I need either all of them to save or none of them (at each interval). I am thinking now of the specific case when the program is closed down inbetween saves of any of these 3 files.

    To try to avoid this I am doing this:
    Code:
    file_A.close(); file_B.close(); file_C.close();
    This will minimise the chance of the program closing out inbetween these file saves but it not guaranteed.

    Is there anyway to guarantee this without having to save all this info in the same file (as it is much more convenient to have 3 separate files)?

    Many thanks

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    It seems like you cannot even guarantee that the writing to the one file will not be cut off in the middle of the process. I suggest you use a folder for each interval so you don't damage previously written files.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Like nvoigt says, if you are trying to protect against any and every possible cause (eg, the power being turned off), there is no way to guarantee three files can be left in a parallel state, so you would have to use three series of multiple snapshots with some kind of indicator at the end showing:

    a) the write was complete
    b) at which stage this write occurred, corroborated to files in the other two series.

    Then at start-up you'd have a routine to assess the three series and select the last three complete parallel records.

    However, if there is a more specific reason for the program "getting cut-off" and you expain that and what OS you're on, there may be simpler options.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I suggest you use a folder for each interval so you don't damage previously written files.
    there may be simpler options.
    Can't only diffs be stored w.r.t some base data, that would not change much ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    You could, but you must still save them in different files in order to be sure.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Have you considered a transactional database?
    Database transaction - Wikipedia, the free encyclopedia

    They've solved all the "did it, didn't it, is it consistent' problems.
    strokebow likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Some great comments. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Temporary lock file access in .net
    By Opariti in forum C# Programming
    Replies: 1
    Last Post: 03-10-2010, 09:20 PM
  2. Saving a file
    By TyPR124 in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2007, 03:26 PM
  3. Saving to a .txt file
    By adr in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2006, 02:14 AM
  4. Running a temporary file
    By Magos in forum C++ Programming
    Replies: 6
    Last Post: 05-27-2004, 05:48 PM
  5. file saving??
    By SuperNewbie in forum Windows Programming
    Replies: 10
    Last Post: 01-19-2004, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21