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. Open Dialog problem

Open Dialog problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
3 Posts 2 Posters 1 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.
  • P Offline
    P Offline
    Plons
    wrote on last edited by
    #1

    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();

    G 1 Reply Last reply
    0
    • P Plons

      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();

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      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 constructed CFileDialog object each time you call FileDlg(), 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 the try/catch block lets you handle any problems with the file access in one place. - The two-argument constructor for the CFile object opens the file. The CFile destructor will automatically close the file when the f object goes out of scope. - The ReportError() method provided by the CFileException 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;

      P 1 Reply Last reply
      0
      • G Gary R Wheeler

        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 constructed CFileDialog object each time you call FileDlg(), 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 the try/catch block lets you handle any problems with the file access in one place. - The two-argument constructor for the CFile object opens the file. The CFile destructor will automatically close the file when the f object goes out of scope. - The ReportError() method provided by the CFileException 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;

        P Offline
        P Offline
        Plons
        wrote on last edited by
        #3

        Thx a lot! I did indeed forgot to declare it, stupid me :-O

        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