Solved

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

Posted on 2004-07-31
5
297 Views
Last Modified: 2010-04-17
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
Comment
Question by:hshliang
  • 3
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11685763
0
 
LVL 8

Expert Comment

by:adg080898
ID: 11689880
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
 
LVL 8

Expert Comment

by:adg080898
ID: 11689916
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
 

Author Comment

by:hshliang
ID: 11696641
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
 
LVL 8

Accepted Solution

by:
adg080898 earned 125 total points
ID: 11698394
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

747 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

9 Experts available now in Live!

Get 1:1 Help Now