Solved

CFile and Power Loss Situations

Posted on 1998-07-24
9
390 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now