Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
366 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

618 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