?
Solved

How do I get the access mode that was used to obtain a pariticular file (or file handle)?

Posted on 2004-07-31
2
Medium Priority
?
359 Views
Last Modified: 2010-04-17
I am trying to write a generic file read programe that accept either a CFile or a file handle.
However, when testing it, I sometimes get error because the file was open in an unexpected mode (like write only etc), hence, I would like to examine the CFile or file handle that is passed to the me and see if it is open appropriately. However, I find that there is NO way to examine the way the file was opened (like with 'r' or 'w' etc).
Can any one tell me if there is a way ( as easy as possible) to examine the mode that was used to open this particular file? to avoid errors.

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
2 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11685759
As you said, there is no way, notice that CFile is an encapsulation of a WinAPI's HANDLE to a file, so both are identical.
WinAPI function GetFileInformationByHandle() doesn't tell you about file access mode, and CFile doesn't save extra information about it, even in its protected members.
0
 
LVL 8

Accepted Solution

by:
adg080898 earned 375 total points
ID: 11692170
You could subclass CFile to intercept the information you desire and store it in your subclass members. This would require a search-and-replace for CFile to whatever you name the subclass, but actual code would not need to be touched.

Something like this:

class MyCFile : public CFile {
protected:
    UINT nMyOpenFlags;
public:
    MyCFile(LPCTSTR pFilename, UINT nOpenFlags);
    BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);

    UINT GetOpenFlags() { return nMyOpenFlags; }
};

MyCFile::MyCFile(LPCTSTR pFilename, UINT nOpenFlags) : CFile(pFilename, nOpenFlags)
{
    nMyOpenFlags = nOpenFlags;
}

BOOL MyCFile::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL)
{
    nMyOpenFlags = nOpenFlags;

    return ((CFile*)this)->Open(lpszFileName, nOpenFlags, pError);
}

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Progress
Starting up a Project
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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