Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CFile and Power Loss Situations

Posted on 1998-07-24
9
Medium Priority
?
399 Views
Last Modified: 2008-03-17
Using the code below I put a breakpoint AFTER the Close(), and I then pull the power on the machine. When I reboot the file has been deleted ! - Is this done by ChkDsk on restart (which is executed when clean shutdown does not take place) ?, Is this a bug ?, and finally is there a way I can prevent the file from being deleted ?

Note : It also has this behaviour when executing a release .exe (i.e. it does not have to be running under MSDEV for this to happen).

CFile f;
char* pFileName = "d:\\nt\\1300\\ned.nt1.1300\\tcq\\PRC00516.001";
strcpy(inp, "hello");
f.Open( pFileName, CFile::modeWrite | CFile::modeCreate );
f.Write( inp, sizeof(inp));
f.Close();
0
Comment
Question by:gbmgeor
  • 5
  • 3
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1168503
The OS caches file and directory information. You didn't let the OS write out the changes.  You might want to try flushing the file (Flush()) before closing it.  However, that may be of limited help.  The OS like any program, functions poorly without electricity.  It may be best to leave the computer plugged in :- )
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168504
Opps I forgot a point I originally planned on mentioning.  If you open a file in a shared mode the OS will not be able to cache as well and most changes will have to be written to disk immediately.  If you use a mode like write exclusive with share read it is the same (as far as your program is concerned) as read and write exclusive.  But the OS will write out changes more quickly.
0
 

Author Comment

by:gbmgeor
ID: 1168505
I agree about switching the power off as not being a very good "test", but if the file has already been Closed(), then surely NT should not go ahead and delete the file. If I break after the close then I can look at the contents of the file, and even delete (del) from within explorer, etc... All I am trying to do is convince a user that the problem is not in my code, but is a "problem" with the OS.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:nietod
ID: 1168506
it is a "problem"  (definitly in quotes) with the OS.  It occurs because the OS caches directory and file system information.  As far as your program or any other program is concerned the file exists bacause they are looking at the cached data, not what is on disk.  Thus NT does not "go and delete the file" it just never gets around to storing the file and its contents in a permanent location--the disk.  Given time it would have.

I recommend you try flushing and opening in a sharable mode, both tend to help the OS get things to disk faster.  (actually they hinder keeping them in memory.)
0
 

Author Comment

by:gbmgeor
ID: 1168507
I always thought that "closing" would flush the buffers. In fact quoting MS doc - "Use the Close member function. This function closes the file-system file and flushes buffers if necessary."

I will try using flush() (can't open in share mode), but I don't hold out much hope.
0
 
LVL 2

Expert Comment

by:warmcat
ID: 1168508
There is a cool FILE_FLAG_WRITE_THROUGH flag you use with the CreateFile() dwFlagsAndAttributes parameter that may help you here.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168509
Unfortunately, in Microsoft's (should I say God-like?) wisdom there was no need provide this flag for the CFile class (as far as I can tell.)    But they did provide the flush() function which should correspond to the windows API FlushFileBuffers() and that should do what you want.  The FILE_FLAG_WRITE_THROUGH would be more convenient, though.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1168510
So did this help?
0
 

Author Comment

by:gbmgeor
ID: 1168511
I've not been able to test using the Flush(), but given time I will give it a try. I had already informed the customer that switching the machine off was not considered a valid test, and if he considered the application to be that mission critical then he should invest in a UPS !

Thanks for your help....

PS : In another application where I use CreateFile() I've already had to use FILE_FLAG_WRITE_THROUGH because of another NT bug when writing to UNC Files :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question