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
308 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
[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
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Simple Linear Regression

630 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