title: check for bad sectors on hard disk
body: <p>Recently after a couple of power outages, my computer has gotten quite messed up (BSODs every now and then, entire OS behaves really strange).</p>
  
  <p>Now I have a program that ran perfectly fine before my computer got messed up. This program just basically creates a file using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx" rel="nofollow noreferrer"><code>CreateFile</code></a> and writes to it using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx" rel="nofollow noreferrer"><code>WriteFile</code></a>. This program also has other stuff doing their thing based on this file. I noticed that the entire program acted strangely as well.</p>
  
  <p>My debugging results: </p>
  
  <ul>
  <li>I noticed that after <code>CreateFile</code>, the file is created where it
  should be (checked using Explorer and command-line).</li>
  <li><code>WriteFile</code> did not write to the file (again checked using Explorer and command-line).</li>
  <li>After <code>WriteFile</code>, whenever I double-clicked on the file in Explorer or did anything with that file, everything behaved as though the file did not exist.</li>
  <li>All APIs returned their respective success codes. No error absolutely.</li>
  <li><p>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:</p>
  
  <p><img src="https://i.stack.imgur.com/E6JQX.png" alt="Error When I Tried Deleting the Folder"></p></li>
  <li><p>The file got deleted though.</p></li>
  </ul>
  
  <p><strong>Note:</strong> The above does not happen always.</p>
  
  <p>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).</p>
  
  <p>Anyway, Microsoft has <a href="http://answers.microsoft.com/en-us/windows/forum/windows_7-windows_programs/error-0x80070091-the-directory-is-not-empty-while/8c58e1d9-b6f2-4164-8fa6-19f35c09ea3d" rel="nofollow noreferrer">this to say about that error</a>. Basically that my hard drive has many bad-sectors.</p>
  
  <p><strong>My question:</strong> 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).</p>
  
  <p>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.</p>