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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange file corruption

Strange file corruption

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
5 Posts 3 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.
  • A Offline
    A Offline
    Artem Moroz
    wrote on last edited by
    #1

    I have a class that represents a log file. This class has two functions and a member variable CFile m_file First is StartLog conains code m_file.Open(.....); Second StopLog contanis code m_file.Close(); And also there is a function AddEvent like this: m_file.Write(.....); m_file.Flush(); I call StartLog when my program starts, and StopLog when it exits. AddEvent is called several times while program runs; It works fine mostly, but there is a problem: File gets corrupted (it contains junk) when the program gets terminated incorrectly. For example when somebody has rebooted computer or terminates it via task manger. (Close() is not get called in this case) Should I reopen and close file every time I need to write to it to ensure safety of the data??? :confused:

    V 1 Reply Last reply
    0
    • A Artem Moroz

      I have a class that represents a log file. This class has two functions and a member variable CFile m_file First is StartLog conains code m_file.Open(.....); Second StopLog contanis code m_file.Close(); And also there is a function AddEvent like this: m_file.Write(.....); m_file.Flush(); I call StartLog when my program starts, and StopLog when it exits. AddEvent is called several times while program runs; It works fine mostly, but there is a problem: File gets corrupted (it contains junk) when the program gets terminated incorrectly. For example when somebody has rebooted computer or terminates it via task manger. (Close() is not get called in this case) Should I reopen and close file every time I need to write to it to ensure safety of the data??? :confused:

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      Yes. Add code in the class destructor to close the file. Kuphryn

      A 1 Reply Last reply
      0
      • V valikac

        Yes. Add code in the class destructor to close the file. Kuphryn

        A Offline
        A Offline
        Artem Moroz
        wrote on last edited by
        #3

        I already did. The problem Is that when the program is terminated incorrectly, the garbage appears inside a file sometimes.... You see, the Close() is not called in such a case :(

        V T 2 Replies Last reply
        0
        • A Artem Moroz

          I already did. The problem Is that when the program is terminated incorrectly, the garbage appears inside a file sometimes.... You see, the Close() is not called in such a case :(

          V Offline
          V Offline
          valikac
          wrote on last edited by
          #4

          Yeah. The only solution is to analyze the file as the program opens it. Do CRC check or simply calculate the bytes and remove all extra data. Kuphryn

          1 Reply Last reply
          0
          • A Artem Moroz

            I already did. The problem Is that when the program is terminated incorrectly, the garbage appears inside a file sometimes.... You see, the Close() is not called in such a case :(

            T Offline
            T Offline
            Terry ONolley
            wrote on last edited by
            #5

            M A V wrote: The problem Is that when the program is terminated incorrectly, the garbage appears inside a file sometimes.... If you manually open the file in append mode, write to the file, and then manually close it every time then you are unlikely to experience the corruption you are talking about. The only way it could get corrupted is if the system were incorrectly shut down during the few milliseconds that the file were open.


            Have you answered an MTQ? Check out the stats!
            What's the latest butt-scratch count? Check it out!

            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