CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2001
    Posts
    3

    Opening a Read Only File with CFile

    Hi,

    I'm trying to open a read only file with CFile. I thought that it would be possible with CFile::modeRead and CFile::shareDenyNone, but I get an access violation when I try and open the file. What am I missing?

    Thanks

    Chris Rombach


  2. #2
    Join Date
    Oct 1999
    Location
    South on the Water
    Posts
    466

    Re: Opening a Read Only File with CFile

    Can't think of anything off hand, but if you Post Some Code and Maybe I can help....

    ++Jyvilian

  3. #3
    Join Date
    Sep 2001
    Posts
    3

    Re: Opening a Read Only File with CFile

    Ok, here's the bit I'm having trouble with. Its the beginning of a function that compares 2 files. I'll post the part that I think is the problem, and if you need more let me know.



    for (int k = i; k < 15; k++)
    {
    CFile ControlFile;
    ControlFile.Open (m_strPathName[0], CFile::modeRead | CFile::shareDenyNone);
    CFile Testfile;
    TestFile.Open (m_strPathName[k], CFile::modeRead | CFile::shareDenyNone);
    ...




    This works great when I'm dealing with non read-only files, but I get an access violation on read-only. Unfortunately my manager wants to use my app on CD's which have read-only files.

    Thanks in advance.

    Chris Rombach


  4. #4
    Join Date
    Oct 1999
    Location
    South on the Water
    Posts
    466

    Re: Opening a Read Only File with CFile

    2 things to try:



    // #1 -- Maybe you need the Exception Parameter?
    CFileException e;
    ControlFile.Open (m_strPathName[0], CFile::modeRead | CFile::shareDenyNone, &e);

    // #2 -- Maybe you need modeReadWrite

    ControlFile.Open (m_strPathName[0], CFile::modeReadWrite, &e);





    ++Jyvilian

  5. #5
    Join Date
    Sep 2001
    Posts
    3

    Re: Opening a Read Only File with CFile

    I tried it and no luck. Any other ideas?

    Chris


  6. #6
    Join Date
    Dec 2001
    Posts
    1

    Re: Opening a Read Only File with CFile

    ControlFile.Open (m_strPathName[0], CFile::modeRead | CFile::shareDenyNone);

    Obviously the CFile::shareDenyNone is the problem.
    You want CFile::modeRead but at the same time you want to write and read also??
    Check the CFIle constructor for detail.


  7. #7
    Join Date
    Aug 2009
    Posts
    5

    Re: Opening a Read Only File with CFile

    Sorry to be bumping up this old thread but I seem to be having a similar problem. The above post seems to indicate that opening a CFile with shareDenyNone and modeRead would not work properly. I was under the impression that this would just mean this instance of the open file would be open for reading only but others could still be opened to write. Is this correct? I've done it in my code and it opens the file, however are there some cases where this would cause errors? Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured