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

Expert Comment

by:Jaime Olivares
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is about my first experience with programming Arduino.
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…

772 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

9 Experts available now in Live!

Get 1:1 Help Now