开发者

WriteFile failure modes

What can happen to a file that is being written to over a network share when the network connection "fails?" I imagine multiple things can happen.

The reason I ask is because a program that I wrote appeared to corrupt a file in a way that I have never seen before. Actually, I have never received a report of my program creating a corrupt file before, and I feel confident if it corrupted a file I would hear about it. The code that that actually generated the corrupt file is actually Microsoft code. The file is created when the save function is called on the IXMLDOMDocument2 interface from the MSXML4.0 COM component. The fu开发者_开发百科nction created an invalid XML document. The last 5452 bytes of a 33789 byte file was filled with NULLs (0x00). I'm trying to understand what happened.


The short answer is that the file can be corrupted (as you've already found).

From there, nearly the only interesting question is how you prevent that from happening again. The typical way is to write the data to a temporary file, then after it's written rename it to the correct name. If the network connection dies in the middle of this, you won't have the correct file name. To recover, you delete any temporary files, and restart the operation from the beginning.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜