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.