views:

402

answers:

2

Assume that write operation throws an exception hafl-way, is there any data written into the file, or there is no data written in the file?

Thank you!

A: 

Short answer: Most likely some data will be written to the file, unless the disk is full at the start of the write operation.

Longer answer: It will depend on what CFileException is thrown from the Write call.

http://msdn.microsoft.com/en-us/library/as5cs056(VS.80).aspx

grepsedawk
A: 

Since you have no view of the internals of CFile (or shouldn't, if it's encapsulated properly), you need to rely on the 'contract' of the API. In other words, unless the documentation tells you specifically what happens in certain cases, you can't rely on it.

Even if you had the source code and could figure it out, the API specification is the contract and anything not specified there can change at any time. This is one reason why some software developers are wary of publishing internals as it then can be seen to lock them in to supporting that forever.

If you really want to ensure that your file will be in a known state after the exception, you will need to code around the behavior. This can be something like:

  • backing up the file at program start-up (simple) ; or
  • backing it up before each save operation (still relatively simple); or
  • backing it up before any write operation (complex and slow).
paxdiablo