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
348 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 125 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…

691 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