Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CFile::Open fails

CFile::Open fails

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionworkspace
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Janine
    wrote on last edited by
    #1

    Hello, This might be a stupid question:-O. I'm trying to save and load settings in my program using CFileDialog. The problem is that opening the file I've saved fails most of the times, but not always. I'm assuming that it is because of a sharing violation, because CFileException gets a value 0xb, which means CFileException::sharingViolation. Please correct me if I'm wrong. lOsError got a value 0x20, but I can't find what that means. So my question is, why does'nt the opening succeed? If it's because of sharing violation, what might be causing it? Not closing the file last time it was used might be, but I think it's something else, because opening fails randomly, like if I try to open it two times a row, the first trying fails and the next one succeeds. Here's how I'm trying to do it:

    void SettingsDlg::OnLoadButton()
    {
    UpdateData();
    CString cstrFileName;
    CFileDialog FileDlg(TRUE, /* Make a open dialog box. */
    "cnf", /* Default file name extension. */
    NULL, /* No default filename. */
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR,
    "Configuration files (*.cnf)|*.cnf|All files (*.*)|*.*||");

    if ( FileDlg.DoModal() == IDOK )
    {
    	cstrFileName = FileDlg.GetPathName();
    
    	/\* Create a new file and open it. \*/
    	CFile\* pFile = new CFile();
    	ASSERT (pFile != NULL);
    
    	CFileException \*pExc = new CFileException(TRUE);
    
    	if ( !pFile->Open(cstrFileName, CFile::modeRead | CFile::shareExclusive, pExc) )
    	{
    		MessageBox("Error: Unable to open the file.");
    		delete pFile;
    		pFile = 0;
    		return;
    	}
    	
    	bool bReading = true;	/\* We'll read. \*/
    	CArchive\* pArchive = NULL;
    	
    	try
    	{
    		pFile->SeekToBegin();
    		UINT uMode = (bReading ? CArchive::load : CArchive::store);
    		pArchive = new CArchive (pFile, CArchive::load);
    		ASSERT (pArchive != NULL);
    	}
    	catch (...)
    	{
    		MessageBox("Error: Unable to read from the file");
    		pFile->Close();
    		delete pFile;
    		pFile = 0;
    		return;
    	}
    
    	Serialize(\*pArchive);
    	
    	UpdateData(FALSE);
    
    	delete pArchive;
    	pArchive = 0;
    
    	pFile->Close();
    
    	delete pFile;
    	pFile = 0;
    }
    

    }

    Please, help me -Janetta

    T 1 Reply Last reply
    0
    • J Janine

      Hello, This might be a stupid question:-O. I'm trying to save and load settings in my program using CFileDialog. The problem is that opening the file I've saved fails most of the times, but not always. I'm assuming that it is because of a sharing violation, because CFileException gets a value 0xb, which means CFileException::sharingViolation. Please correct me if I'm wrong. lOsError got a value 0x20, but I can't find what that means. So my question is, why does'nt the opening succeed? If it's because of sharing violation, what might be causing it? Not closing the file last time it was used might be, but I think it's something else, because opening fails randomly, like if I try to open it two times a row, the first trying fails and the next one succeeds. Here's how I'm trying to do it:

      void SettingsDlg::OnLoadButton()
      {
      UpdateData();
      CString cstrFileName;
      CFileDialog FileDlg(TRUE, /* Make a open dialog box. */
      "cnf", /* Default file name extension. */
      NULL, /* No default filename. */
      OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR,
      "Configuration files (*.cnf)|*.cnf|All files (*.*)|*.*||");

      if ( FileDlg.DoModal() == IDOK )
      {
      	cstrFileName = FileDlg.GetPathName();
      
      	/\* Create a new file and open it. \*/
      	CFile\* pFile = new CFile();
      	ASSERT (pFile != NULL);
      
      	CFileException \*pExc = new CFileException(TRUE);
      
      	if ( !pFile->Open(cstrFileName, CFile::modeRead | CFile::shareExclusive, pExc) )
      	{
      		MessageBox("Error: Unable to open the file.");
      		delete pFile;
      		pFile = 0;
      		return;
      	}
      	
      	bool bReading = true;	/\* We'll read. \*/
      	CArchive\* pArchive = NULL;
      	
      	try
      	{
      		pFile->SeekToBegin();
      		UINT uMode = (bReading ? CArchive::load : CArchive::store);
      		pArchive = new CArchive (pFile, CArchive::load);
      		ASSERT (pArchive != NULL);
      	}
      	catch (...)
      	{
      		MessageBox("Error: Unable to read from the file");
      		pFile->Close();
      		delete pFile;
      		pFile = 0;
      		return;
      	}
      
      	Serialize(\*pArchive);
      	
      	UpdateData(FALSE);
      
      	delete pArchive;
      	pArchive = 0;
      
      	pFile->Close();
      
      	delete pFile;
      	pFile = 0;
      }
      

      }

      Please, help me -Janetta

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      The error codes you've posted are indeed related to sharing violation - some other process has the file open, and you're trying to access the exclusive access to file (CFile::shareExclusive flag does this). You may check www.sysinternals.com[^] and download their 'Handle' utility which displays information about open files. On the other hand, you may just drop 'shareExclusive' flag. BTW: why on Earth are you allocating your CFile and CArchive objects on the heap? You can just use local variables. Tomasz Sowinski -- http://www.shooltz.com

      Free your mind and your ass will follow.

      J 1 Reply Last reply
      0
      • T Tomasz Sowinski

        The error codes you've posted are indeed related to sharing violation - some other process has the file open, and you're trying to access the exclusive access to file (CFile::shareExclusive flag does this). You may check www.sysinternals.com[^] and download their 'Handle' utility which displays information about open files. On the other hand, you may just drop 'shareExclusive' flag. BTW: why on Earth are you allocating your CFile and CArchive objects on the heap? You can just use local variables. Tomasz Sowinski -- http://www.shooltz.com

        Free your mind and your ass will follow.

        J Offline
        J Offline
        Janine
        wrote on last edited by
        #3

        Tomasz Sowinski wrote: On the other hand, you may just drop 'shareExclusive' flag. I'm using that already, or am I doing it in a wrong way? It's used like this: if ( !pFile->Open(cstrFileName, CFile::modeRead | CFile::shareExclusive, pExc) ) I'll check the address you gave. Thanks, -Janetta

        T 1 Reply Last reply
        0
        • J Janine

          Tomasz Sowinski wrote: On the other hand, you may just drop 'shareExclusive' flag. I'm using that already, or am I doing it in a wrong way? It's used like this: if ( !pFile->Open(cstrFileName, CFile::modeRead | CFile::shareExclusive, pExc) ) I'll check the address you gave. Thanks, -Janetta

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          Sorry - I wasn't too clear. By dropping I meant removing the flag and using CFile::modeRead without shareExclusive or modeRead with shareDenyNone. Tomasz Sowinski -- http://www.shooltz.com

          Free your mind and your ass will follow.

          J 1 Reply Last reply
          0
          • T Tomasz Sowinski

            Sorry - I wasn't too clear. By dropping I meant removing the flag and using CFile::modeRead without shareExclusive or modeRead with shareDenyNone. Tomasz Sowinski -- http://www.shooltz.com

            Free your mind and your ass will follow.

            J Offline
            J Offline
            Janine
            wrote on last edited by
            #5

            Ok, now I understood. And it helped. I'll go through the link later anyway, it seemed interesting. Thanks. -Janetta

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups