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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about problems I had with the new location API and permissions in Marshmallow
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

734 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