Occasionally we get reports of corrupted files from customers. These are rare and next-to-impossible to reproduce. When we log into the customers' machine remotely we find the file (usually an INI file) cannot be opened. Nor can the file be copied locally for analysis. Based on limited examples, these corrupted files have a size of "0kB" and can only be deleted. When this occurs, it appears to affect just one INI file out of dozens. The rest of the system is fine. Once the corrupted file is re-created, the system fully recovers.
We do not know how this happens and would like to reproduce and hopefully resolve the issue. Could be a Windows issue, a coding error, even a hardware issue related to powering down the machine unexpectedly. For test purposes, I've created a TXT file with random garbage as in:
// Delphi code, but applies to any language
procedure TfrmDiagnostics.CreateCorruptFile();
var
fpDump: TextFile;
nChar: Integer;
begin
RandSeed := (GetTickCount() mod 23);
AssignFile(fpDump, 'C:\Corrupted.txt');
for nChar:=1 to 100 do
Write(fpDump, Chr(Random(250)));
Flush(fpDump);
CloseFile(fpDump);
end;
This does generate a nicely mangled file, the first few bytes were: NREâA#»Bâ÷lÁb…ilL(‘—ØÏD YL&d.... But the file still opens in Notepad++ and a few other ASCII editors I tried.
Is it possible to create a genuinely "corrupt" file in Windows, one that Windows refuses to open? This possibly involves fiddling with the file-allocation table, but I doubt users have any access to the FAT (and it doesn't sound like a safe thing to do anyway).