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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

How can I examine the access mode that was used to open a particular CFile or file handle.

I am writing a program to read a CFile or file handle that is passed into the function.
I find that if the file is opened in a wrong mode, I will get an error.
To avoid handling an error and fails I want to examine the CFile or file handle that was passed in.
So if the file is not opened appropriately, I can close the file and reopen it in the appropriate mode.

Is there a way I can examine and find out HOW was a particular CFile or file handle opened?

thank you
0
hshliang
Asked:
hshliang
  • 3
1 Solution
 
Jaime OlivaresCommented:
0
 
adg080898Commented:
You are avoiding the inevitable. Errors WILL occur. Avoding one kind of error does not get you off the hook for error checking. Any code worth writing MUST handle errors.

I was unable to locate a standard Windows API function to get writability information. You could probably write zero bytes and test for an error (this won't set end of file like it did in DOS), but again, you should handle the error in the real code, not try to avoid handling the error! What if the file was opened from a CD ROM? You can open it any way you like and it will still be an error to write to it. What about a file opened across a network with read-only permissions? What about writing to a floppy that is bad? Errors happen everywhere.
0
 
adg080898Commented:
Be careful about closing and re-opening it. If it is a multi-user application, you will release all file locks. If the file was opened as a temporary file, it will be deleted when closed.
0
 
hshliangAuthor Commented:
Thank you adg. It is true that I want to avoid "failing" in the function as much as possible as the user may not be able to handle the "error", while I may be able to HANDLE it programatically. Even if it fails to write, I will not be able to examine the actual reason. If I can examine the file handle and tell that it was opened with a 'r' mode only, then I may suggest some solution to the user to avoid this error again.

I think it is a very valid suggestion of NOT to reopen the file (Except I found a ReoprnFile function that will work only in Windows 2003 Server, hopefully we will find such a function in other Windows.)

If there is no such function, I think I will have to live with failing the function and let the user to figure out the problem.

hshliang
0
 
adg080898Commented:
There is a very similar question I remember responding to recently. I suggested subclassing CFile to intercept the mode from the constructor and the open method:

http://www.experts-exchange.com/Programming/Q_21078122.html
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now