问题
Recently after a couple of power outages, my computer has gotten quite messed up (BSODs every now and then, entire OS behaves really strange).
Now I have a program that ran perfectly fine before my computer got messed up. This program just basically creates a file using CreateFile and writes to it using WriteFile. This program also has other stuff doing their thing based on this file. I noticed that the entire program acted strangely as well.
My debugging results:
- I noticed that after
CreateFile
, the file is created where it should be (checked using Explorer and command-line). WriteFile
did not write to the file (again checked using Explorer and command-line).- After
WriteFile
, whenever I double-clicked on the file in Explorer or did anything with that file, everything behaved as though the file did not exist. - All APIs returned their respective success codes. No error absolutely.
After I stopped my program and exited the IDE, I went to explorer and tried to delete the folder which contains the file created above, it showed me this:
The file got deleted though.
Note: The above does not happen always.
Due to the above debugging, I came to the conclusion that thanks to my entire OS acting strange, my program also acts strangely. No fault in my program (since everything worked perfect before those darned power outages).
Anyway, Microsoft has this to say about that error. Basically that my hard drive has many bad-sectors.
My question: Is there any way to programmatically check if a hard drive is fine in C? This detection need not be 100% correct. I do, however, want this check to be quite fast (since I do a lot of writes and such).
I want this check because as of now, the rest of my program thinks that the file is there and ready (because nothing returned an error) and all sorts of weird behaviour ensues. As a user who might not know that his/her hard-drive is zonked, I would like my program to suggest running a disk check.
回答1:
You might be better off looking at S.M.A.R.T prefailure notifications, through an API that allows early warning of drive issues before they occur. Here is one implementation I found quickly which appears to be for your platform, but I have not tested it.
I suspect there are better ones available. The main advantage of this SMART approach is finding issues before they become important. However, many operating systems have this functionality built in, it's rare to do it at the application level in something other than a disk utility.
回答2:
As your application is built on top of the OS, you have to rely on its error reporting, and pass them up. You can't assume that particular errors mean it needs a disk check.
In your case, I recommend running a full disk check and carry on as normal.
来源:https://stackoverflow.com/questions/15678703/check-for-bad-sectors-on-hard-disk