CFile::Open fails
-
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
-
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
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.
-
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.
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 -
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, -JanettaSorry - 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.
-
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.