Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CFile and Power Loss Situations

Posted on 1998-07-24
9
Medium Priority
?
398 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

715 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