Short version:

Windows 7 appears to be losing changes made to text files if the PC loses power, even if the changes have been saved. Why would it be doing this and how can I make the changes stick? I need the changes to be persistent even if there is a power failure.

More Details:

I did all sorts of testing with opening, modifying, saving, etc. text files (specifically, a configuration file with pieces of data on each line) in combination with shutting down properly and unplugging the PC in different orders and it looks like the following patterns emerged:

  • text files can be opened and viewed only and the PC shut down properly OR lose power and the file is OK
  • text files can be opened, modified and saved and the PC shut down properly and the file is OK
  • text files are opened, modified (change a line of data) and saved via notepad and the PC loses power (i.e. unplug it, not shut down properly) and when it powers back up the file is still there but the changes are gone.
  • If the file is modified by the software that uses it (which completely rewrites it) and there is a power failure, when it comes back up the file only consists of a bunch of "space" characters all one one line.
  • One time I deleted all lines of data and re-wrote the file manually via notepad and unplugged it and when it powered back up the first 70% or so of the file was there but the end was gone.
  • If the text file is modified and then I wait for a period (5 minutes) and there is a power failure (unplug), when it starts back up, the text file is fine and the changes are intact.

So, I guess that every so often Windows does something that makes the changes permanent and this same thing happens during shut-down and whatever this thing is that it does gets skipped/missed if there is a power failure shortly after the changes are made. Is that correct?

What is the thing that it is doing that makes the changes permanent (it's obviously not simply the act of saving the file)? How often does it occur? Is it possible to make it occur more often?


Recommended Answers

All 12 Replies

a bit of a guess but if the file that you are changing is one that affects the registry (or is called by the registry) it may be that this (the registry) is changing back to its file it can register as registry files (and internal registry changes) are not (saved) carried out until a proper full shut down which rubs out the old (current) registry files and tells the machine to use the new (modified) ones at next full clean shut down and proper start up. (interruption of shut down will revert back to the old file and not use the new one.

Thanks for the reply, mjdodd. The file is just a plain text file that lives in the same directory as the executable that contains some lines of data that the software uses to store some user settings. When I modify it with notepad, I just make changes to it, save it and close it. When the software uses it, it just opens it, does a bunch of vc++ WriteString operations and closes the file.

Evidently just saving the file and closing it doesn't make the changes permanent right away but after some period of time they are made permanent (maybe it's written into some cache at first and every so often Windows writes that cache to disk?)

I suspect that the software that uses this file that you have problems with actually stores the values of that data in the registry ... see above!! (ie it takes the input and tells the registry there are new bits of data in the file and then the registry is only updated .... as above

I suspect that the software that uses this file that you have problems with actually stores the values of that data in the registry ... see above!! (ie it takes the input and tells the registry there are new bits of data in the file and then the registry is only updated .... as above

I wrote the software and it doesn't explicitly access the registry; could windows be accessing the registry using that data? I read values from the file and use them to initialize text edit boxes, combo/drop-down boxes, etc. in the software user interface and that's it. It also happens when I don't even use the software and the file is just sitting on the desktop as a text file that I edit with notepad (the text file only gets accessed either when I run the software and modify the user settings or open it with notepad and this behavior happens in both instances). AFAIK, the registry shouldn't ever be in the picture.

my knowledge is general not specific but I do know this much!
The registry is constantly being changed added to etc by a large number of programs many times in the background.
Two examples: Any change that you do in control panel is changing the registry behind the scenes. Many other programs do the same without being visible on the screen and any change of hardware also changes the registry. many config type files, .reg files, .ini , .exe , .dat and even data files may be accessed and used by the registry. The registry is the processors database (not of what to do that's the program & OS as you know, but is the reservoir of data about everything that goes on or resources available in the machine that a program or OS demands of the processor
Any 'restore' that you do is actually amongst other things bringing back a previous copy of the registry as as that time it worked!
Having said that it may now be obvious that even simple programs like notepad will pass commands and data (if only the address of items on the hard drive to the processor and that in turn will use the registry to find out what facilities are available and where to find them to carry out the programs requirements.
So as a simple example in your case though I am theorising a bit you suggest you use combo boxes and he only differentiating item of those is where they appear on the screen and the code that you write behind those. So how does the processor know where to find what that combo box actually does, first stop video memory to find the exact position on screen, second stop .......... yes you guessed it the registry to find out where the code that you put behind the box is actually stored (you think it is in your program and of course that is where the registry tells the processor to find it.

I have taken liberties to simplify this you will know more about coding and ma

oops sorry pressed the wrong key
than I do but the passing of such data items is quite complex and beyond the scope of exploring here any further.
Hope this is not too much knowledge but it may give insights as to why I think you are getting this problem.
There may of course be a simpler explanation like a virus!!


Thank you for the additional information about what the registry is and how it is used, but I'm still not convinced that it's a registry issue. Especially if the systems needs a good, clean shutdown and boot to make registry changes permanent.

The pattern I see is the following:
If a change to the text file is made and saved and then the power is lost (no clean shutdown and boot) immediately, the changes are lost. If the text file is changed and saved and then a short period of time elapses before the power is cut (again, no clean shutdown and boot process) the changes are still there.

That seems (IMO) not to point to some registry changes that requires a shutdown and reboot to become permanent but rather to some latency between saving the file to disk (via the save command) and it actually being written to disk [i.e. the save command saves the file, but that saved copy of the file is in RAM and the copy on the HDD is still the original, unmodified file and at some point after that (say, X ms after the file is closed) the saved copy from RAM is written to the HDD and purged from RAM.] It's been a while since I took any OS, microprocessor or other architecture classes, so I'm not crystal clear on how all that works, but that seems to me to be what it's doing.

Does that behavior exist? Basically: when you save a text document, are the changes A) immediately written to the file on disk or are they B) saved in RAM and the copy from RAM periodically (or at some point) written to the disk?

This is occurring on a machine with Win 7 on an Intel Atom board with a WD Scorpio Blue HDD, in case any of that matters.

Still haven't figured this out yet :(

another thought but really a last one from me!
If your machine is short of memory it could be writing the changes temporarily to the page file (on disk or even other partition) and then transfering to disk when time / memory becomes available. used to happen on old machines but not seen it on recent ones!
best of luck

I found this explanation on another site about write-caching to hdd's

Disk Write Caching is a feature which allows you to increase the performance of your Hard Disk. While this can improve performance, it comes attached with its own risk. If the system or disk loses power then there is every possibility that the data gets corrupted or lost in the event of a power failure. Depecnding upon the criticality of the data and the reliability of the power and power backups and UPS, enabling or disabling Disk Write Caching should be enabled or disabled.

You can see the System rating for your Disk changes before and after enabling/disabling Disk Write Caching

Windows 7 and Windows Server 2008, by default enables Disk Write Caching. However, to enable or disable Disk Write Caching,

1. Click Start, right-click Computer and select Properties.

2. In the leftpane, click Device Manager.

3. Expand Disk Drives and right-click on the disk where you need to enable or disable Disk Write Caching and click properties.

4. Click the Policies tab and check “Enable Write Caching on the device” to enable (default). To disable, uncheck the box.

commented: Great. Exactly the info I was looking for (I think) ! +2

I found this explanation on another site about write-caching to hdd's

Please include a link to that post.

I found this explanation on another site about write-caching to hdd's

I don't know why I didn't think of that setting. I usually just think of it in terms of flash drives or external HDDs. I disabled write-caching and now if power is lost after a five- or six-count of saving changes they are persistent (whereas before it was much longer). Earlier than that, though, and they still sometimes get lost/file is corrupted so there may be another cache in the picture somewhere. I'm not sure I'm going to be able to improve it any more than that though.

Turn off, write-cache; make sure there's a UPS in the picture; cross fingers.

Thanks everyone who responded.

Also, I searched the text above and found it at this link:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.