Open Dialog problem
-
Hi, I'm having a problem opening a file. GetFileName() doesn't seem to give me a name. If I open a file, the dialog appears, I select a file and my code returns a blank message box and then a messagebox with no error happened. I'm kinda new to all this MFC stuff (and my C++ skill isn't that good) so I really don't know what's wrong. :confused: if( FileDlg().DoModal() == IDOK ) { CFileException ex; if( f.Open(FileDlg().GetFileName(), CFile::modeRead) == FALSE ) { AfxMessageBox(FileDlg().GetFileName()); TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); AfxMessageBox(szError); return; } AfxMessageBox("OK"); CArchive test(&f, CArchive::load); CLoader2Doc::Serialize(test); } else return; f.Close();
-
Hi, I'm having a problem opening a file. GetFileName() doesn't seem to give me a name. If I open a file, the dialog appears, I select a file and my code returns a blank message box and then a messagebox with no error happened. I'm kinda new to all this MFC stuff (and my C++ skill isn't that good) so I really don't know what's wrong. :confused: if( FileDlg().DoModal() == IDOK ) { CFileException ex; if( f.Open(FileDlg().GetFileName(), CFile::modeRead) == FALSE ) { AfxMessageBox(FileDlg().GetFileName()); TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); AfxMessageBox(szError); return; } AfxMessageBox("OK"); CArchive test(&f, CArchive::load); CLoader2Doc::Serialize(test); } else return; f.Close();
How is
FileDlg()
declared? Unless it's a reference to an existing file dialog object, I doubt it's going to give you the results you expect. It sounds like you're getting a freshly constructedCFileDialog
object each time you callFileDlg()
, which would return a blank filename. The conventional way to do this sort of thing is as follows:CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDOK) {
try {
CFile f(dlg.GetPathName(),CFile::modeRead);
while (f.Read(...)) {
}
}
catch (CFileException *e) {
e->ReportError();
e->Delete();
}
}Some notes on the snippet of code: - You can add arguments to the
CFileDialog
constructor to specify the default file extension, filename, and so on. - Embedding all of the file operations in thetry/catch
block lets you handle any problems with the file access in one place. - The two-argument constructor for theCFile
object opens the file. TheCFile
destructor will automatically close the file when thef
object goes out of scope. - TheReportError()
method provided by theCFileException
object displays an appropriate error message for you. The nice thing here is, you get the standard Windows error messages, appropriate to the type of error, and they're in the language of the user.
Software Zen:
delete this;
-
How is
FileDlg()
declared? Unless it's a reference to an existing file dialog object, I doubt it's going to give you the results you expect. It sounds like you're getting a freshly constructedCFileDialog
object each time you callFileDlg()
, which would return a blank filename. The conventional way to do this sort of thing is as follows:CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDOK) {
try {
CFile f(dlg.GetPathName(),CFile::modeRead);
while (f.Read(...)) {
}
}
catch (CFileException *e) {
e->ReportError();
e->Delete();
}
}Some notes on the snippet of code: - You can add arguments to the
CFileDialog
constructor to specify the default file extension, filename, and so on. - Embedding all of the file operations in thetry/catch
block lets you handle any problems with the file access in one place. - The two-argument constructor for theCFile
object opens the file. TheCFile
destructor will automatically close the file when thef
object goes out of scope. - TheReportError()
method provided by theCFileException
object displays an appropriate error message for you. The nice thing here is, you get the standard Windows error messages, appropriate to the type of error, and they're in the language of the user.
Software Zen:
delete this;