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
300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 57
array6 challenfge 6 83
How to get boolean result of md5sum from two files in powershell 8 73
Device same like our heart 12 79
This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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