Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Data corruption during File Write

Data corruption during File Write

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SelvaKr
    wrote on last edited by
    #1

    Hi All, I am having an application where a structure will be directly written into HDD in binary format at a particular time interval. But if PC gets turned off due to some abnormal problems, during file write, after the next restart the entire file has only 0's. it is getting corrupted. How can i avoid this problem (apart from taking backup and restore in case of corruption). Any help would be appreciated. Thanks.

    Selva

    N J B 3 Replies Last reply
    0
    • S SelvaKr

      Hi All, I am having an application where a structure will be directly written into HDD in binary format at a particular time interval. But if PC gets turned off due to some abnormal problems, during file write, after the next restart the entire file has only 0's. it is getting corrupted. How can i avoid this problem (apart from taking backup and restore in case of corruption). Any help would be appreciated. Thanks.

      Selva

      N Offline
      N Offline
      Nelek
      wrote on last edited by
      #2

      If the file is very big one way could be dividing the data to be written in small packets, opening file, saving a packet, closing file and repeating. It will be slower, but closing the file sometimes during the process should make to save the already sent content, you will lose only the packet you are sending in the moment the computer crashes. BTW It is just a guess, never tried it.

      Greetings. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson

      1 Reply Last reply
      0
      • S SelvaKr

        Hi All, I am having an application where a structure will be directly written into HDD in binary format at a particular time interval. But if PC gets turned off due to some abnormal problems, during file write, after the next restart the entire file has only 0's. it is getting corrupted. How can i avoid this problem (apart from taking backup and restore in case of corruption). Any help would be appreciated. Thanks.

        Selva

        J Offline
        J Offline
        JudyL_MD
        wrote on last edited by
        #3

        An alternative to Nelek's suggestion of Open, Write, Close is to call a Flush after every write. There always is a window of opportunity for disk files to get trashed. Even using the suggestions you've received, there is a chance that the machine will power down before the disk cache is written to disk. The only way to make that window smaller is to use something like the commit function, which is a pretty low level C function. It instructs the OS to issue the actual write commands to the disk. The one case you can't guard against is someone powering off a machine while the actual physical write is in progress, but from what little you said, it sounds flush should give you what you need. Judy

        1 Reply Last reply
        0
        • S SelvaKr

          Hi All, I am having an application where a structure will be directly written into HDD in binary format at a particular time interval. But if PC gets turned off due to some abnormal problems, during file write, after the next restart the entire file has only 0's. it is getting corrupted. How can i avoid this problem (apart from taking backup and restore in case of corruption). Any help would be appreciated. Thanks.

          Selva

          B Offline
          B Offline
          Bram van Kampen
          wrote on last edited by
          #4

          SelvaKr wrote:

          after the next restart the entire file has only 0's. it is getting corrupted.

          I sense that you are after a way of writing mission critical data in an guaranteed safe way, so that integrity is maintained, even during a powerfailure halfway tru the write. I have done quite a bit of research on this,and came to the following design conclusions.- 1. It is Not possible to guarantee a successfull write under above circumstances 2. It is possible as Second Best, to maintain a consistent state. This means that under such circumstances, the transaction is either completed in full, or you are returned to the state prevailing before the transaction commenced. This is not a guaranteed write, but, atleast, you have not the corruption of a partially completed transaction. My system requires two identical file sets, marked Primary and Alternate. There is also a single third file, the State file, which holds only One Byte. This Byte assumes one of the following Values:- STATE_NORMAL, STATE_MOD_PRIMARY, STATE_MOD_ALTERNATE. When we start, Both Primary ad Alternate Sets are Identical. Step 1: The Write Operation commences by opening the State File, and Writing STATE_MOD_PRIMARY to it, after which it is closed. This indicates that the Primary Set is now entering into a potentially unstable State, but the Alternate holds a consistent backup. Step 2: Carry Out All your Writes to the Primary Set. When the Write is Completed, Close All Files. Step 3: Open the State File, and Write STATE_MOD_ALTERNATE to it, after which it is closed. This indicates that the Alternate Set is now entering into a potentially unstable State, but the Primary Set holds the New Dataset. Step 4: Delete All Alternate Files, and replace them with copies of the Primary Set. Step 5: Open the State File, and Write STATE_MOD_NORMAL to it, after which it is closed. This indicates that the Data Set is Once again in a Stable state. When your Excecutable starts, it must investigate the FileTime's of the State File, the Primary Set, and the Alternate Set. A Prima Facia Consistent state is indicated by FileTime(Primary) Another Consistency test is that the State File contains STATE_MOD_NORMAL If any of this fails, you can figure out from the above description, where things

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups